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报错说没找到相关类之前,我们仍然有一个机会去加载相关类。