是否为自动加载的类的父类调用__autoload()? [英] Is __autoload() called for parent classes of autoloaded classes?

查看:58
本文介绍了是否为自动加载的类的父类调用__autoload()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main.php中,将添加自动加载并创建一个新对象:

In main.php, autoload is added and a new object is created:

function __autoload($class) {
    require_once($class . '.php');
}
...
$t = new Triangle($side1, $side2, $side3);

Triangle.php中:

class Triangle extends Shape {...}

Shape.php是一个抽象类:

abstract class Shape {
    abstract protected function get_area();
    abstract protected function get_perimeter();
}

我可以看到__autoload函数调用了Triangle.php,但是它同时调用了Shape.php吗?

I can see that __autoload function calls Triangle.php, but does it call Shape.php at the same time?

推荐答案

否(不是同时在 exact 上),但是可以(它将加载并且一切正常).

No (not at the exact same time), but yes (it will get loaded and everything will work).

当您调用new Triangle时,将看到Triangle是尚未加载的类,因此它将调用__autoload().然后将require_once Triangle.php文件.

When you call new Triangle it will see that Triangle is a class which hasn't been loaded yet, so it calls __autoload(). This will then require_once the Triangle.php file.

在解析Triangle.php时,它发现还有一个尚未加载的类(Shape),因此它重复了该过程.

In parsing Triangle.php it sees that there's another class which hasn't been loaded (Shape) so it repeats the process.

简而言之,您所要做的无非是要完成的事情,但这要经过很多遍.

In short, there's nothing more you need to do than what you've got, but it does it in a number of passes.

这篇关于是否为自动加载的类的父类调用__autoload()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆