PHP spl_autoload [英] PHP spl_autoload

查看:94
本文介绍了PHP spl_autoload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的没有得到spl_autoload的文档

i dont really get the docs for spl_autoload

bool spl_autoload_register  ([ callback $autoload_function  ] )

据我了解,它将尝试运行php遇到尚未加载的类时注册的功能.例如

from my understanding, it will try to run functions registered when php comes across a class not loaded already. so example,

public function autoload() {
    require ('nonLoadedClass.php');
}
spl_autoload_register(autoload);
$x = new nonLoadedClass();

会导致需求运行吗?这样我还可以注册许多自动加载功能?

will cause the require to run? so i can also register many autoload functions?

public function autoloadXXX() {...}
public function autoloadYYY() {...}
public function autoloadZZZ() {...}
spl_autoload_register('autoloadXXX');
spl_autoload_register('autoloadYYY');
spl_autoload_register('autoloadZZZ');

对于学说,

require_once(dirname(__FILE__) . '/lib/vendor/doctrine/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));

传递了一个数组,所以我想它将尝试在Doctrine类中运行autoload函数(这是必需的)?

an array is passed, so i guess it will try to run the autoload function within the Doctrine class (which was required)?

推荐答案

spl_autoloader_register registers a callback function/method, that will be called when your code is trying to use a not-known class.

可以通过几种方式描述回调函数:

A callback function can be described in several ways :

  • 一个简单的函数名称:'my_function'
  • 类的静态方法:array('MyClass', 'myMethod')
  • 类实例的方法:array($myObject, 'myMethod')
  • a simple function name : 'my_function'
  • a static method of a class : array('MyClass', 'myMethod')
  • a method of an instance of a class : array($myObject, 'myMethod')

以Doctrine为例,这似乎是第二种解决方案:如果使用了PHP不知道的类,则自动加载器将调用Doctrine::autoload,并将其类名称作为参数传递.

In the case of Doctrine, it seems to be the second solution : if a class that PHP doesn't know is used, the autoloader will call Doctrine::autoload, passing it the class name as a parameter.


这样我也可以注册许多自动加载 功能?

so i can also register many autoload functions?

是的,您可以使用 spl_autoload_register :

Yes, you can, with spl_autoload_register :

如果必须有多个自动加载 功能,spl_autoload_register() 允许这一点.有效地 创建自动加载功能队列, 并贯穿其中的每个 定义它们的顺序.

If there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined.

但是我们通常不会为我们拥有的每个类定义一个自动加载函数/方法;我想那将是非常低效的.

But we don't generally define one autoloading function/method for each class we have ; that would be quite inefficient, I suppose.

相反,我们使用自动加载功能接收的类名来确定应包含哪个文件.

Instead, we use the class-name, which is received by the autoloading function, to decide which file should be included.

例如,自动加载功能可能看起来像这样:

For instance, an autoload function might look like this :

function my_autoloader($className)
{
    require LIBRARY_PATH . '/' . $className . '.php';
}

诀窍在于,如果您的文件是根据它们所包含的类来命名的,则立即变得容易得多;-)

The trick being that if your files are named after the classes they contain, it immediatly becomes much easier ;-)

这就是为什么通常使用PEAR约定来命名类的原因:My_Class_Name映射到文件My/Class/Name.php

This is why classes are often named using the PEAR convention : My_Class_Name maps to the file My/Class/Name.php

这篇关于PHP spl_autoload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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