Autoloading Classes(自动加载所需类)

Many developers writing object-oriented applications create one PHP source file per-class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).

In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

 

很多面向对象程序开发人员习惯每个PHP文件创建一个类。这样做有一个弊端,就是如果当前类牵涉到很多其他类,那么在文件开头,就必须includes所有相关类所在的PHP文档。

在PHP中,就不必这样了。我们可以定义一个__autoload()函数。如果我们用到了一个没有定义过的类,就会调用这个函数。这样,在PHP报错说没找到相关类之前,我们仍然有一个机会去加载相关类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值