Home  >  Article  >  Backend Development  >  PHP继承提示找不到父类

PHP继承提示找不到父类

WBOY
WBOYOriginal
2016-06-06 20:17:592442browse

直接上代码吧。

<code>namespace 'Test';
class A
{
    
}</code>
<code>namespace 'Test';
//直接运行B会报错,提示找不到AA
include 'A.php';
//加上include这句后,就可以了
class B extends A
{
    
}</code>

这个是为什么呢?俩个文件在同一个文件夹下,也在同一个目录下,不include也应该没问题,框架中常用的继承也没有include。

回复内容:

直接上代码吧。

<code>namespace 'Test';
class A
{
    
}</code>
<code>namespace 'Test';
//直接运行B会报错,提示找不到AA
include 'A.php';
//加上include这句后,就可以了
class B extends A
{
    
}</code>

这个是为什么呢?俩个文件在同一个文件夹下,也在同一个目录下,不include也应该没问题,框架中常用的继承也没有include。

那是因为框架在前面已经注册了自己的autoload,所以当你需要A时如果当前文件中没有A这个类,那么就触发autoload机制,通过autoloadA类所对应的文件include进来。

多学学基础吧,不include怎么会能用呢,框架里肯定也是给include了,只不过是给封装好了

不使用include的 会调用autoload 到默认的文件位置寻找A类吧,很明显 当前文件夹 并不在默认位置?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn