简体   繁体   中英

Class Extends In Php

I have 3 pieces of the file

1. a.php

    <?
    class a {
         public function name () {
             return "My name is ukungzulfah";
         }
    }
    ?>

2. b.php

    <?
    class b extends a {
         function na () {
             echo $ this-> name ();
         }
    }

    ?>

3.c.php

    <?
    include "a.php";
    include "b.php";

    $ te = new b;
    echo $ te:: na ();

    ?>

The result is an error: Fatal error: Using $ this Pls note in object context in C: \\ xampp \\ htdocs \\ sampleNetbeans \\ controller \\ welcome.php on line 4.

Is there something wrong with the code above. if called from b.php was no problem, the problem only if the call by c.php file.

You are using static syntax ( $object::method() ) when you need to use instance syntax ( $object->method() ).

Try echo $te->na(); instead of echo $te::na(); .

This is the only problem I can see in the code you have pasted, except for the unusual whitespace ( $ te and $ te:: na () ) which actually causes compiler errors for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM