简体   繁体   English

类在PHP中扩展

[英]Class Extends In Php

I have 3 pieces of the file 我有3个文件

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. 结果是一个错误:致命错误:在第4行的C:\\ xampp \\ htdocs \\ sampleNetbeans \\ controller \\ welcome.php中的对象上下文中使用$ this Pls注释。

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. 如果从b.php调用没有问题,则只有通过c.php文件调用才有问题。

You are using static syntax ( $object::method() ) when you need to use instance syntax ( $object->method() ). 当您需要使用实例语法( $object->method() )时,可以使用静态语法( $object::method() $object->method() )。

Try echo $te->na(); 尝试echo $te->na(); instead of echo $te::na(); 而不是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. 这是我可以在您粘贴的代码中看到的唯一问题,除了异常的空格( $ te$ te:: na () )外,这实际上会导致我出现编译器错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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