简体   繁体   English

PHP spl_autoload

[英]PHP spl_autoload

i dont really get the docs for spl_autoload 我真的没有得到spl_autoload的文档

bool spl_autoload_register  ([ callback $autoload_function  ] )

from my understanding, it will try to run functions registered when php comes across a class not loaded already. 根据我的理解,当php遇到未加载的类时,它将尝试运行已注册的函数。 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');

in the case of doctrine, 在学说的情况下,

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

an array is passed, so i guess it will try to run the autoload function within the Doctrine class (which was required)? 一个数组被传递,所以我想它会尝试在Doctrine类中运行自动加载功能(这是必需的)?

spl_autoloader_register registers a callback function/method, that will be called when your code is trying to use a not-known class. spl_autoloader_register注册一个回调函数/方法,当您的代码尝试使用未知类时将调用该函数/方法。

A callback function can be described in several ways : 回调函数可以用几种方式描述:

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

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. 在Doctrine的情况下,它似乎是第二种解决方案:如果使用PHP不知道的类,自动加载器将调用Doctrine::autoload ,并将类名作为参数传递给它。


so i can also register many autoload functions? 所以我还可以注册许多自动加载功能?

Yes, you can, with spl_autoload_register : 是的,您可以使用spl_autoload_register

If there must be multiple autoload functions, spl_autoload_register() allows for this. 如果必须有多个自动加载功能, spl_autoload_register()允许这样做。 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 ;-) 诀窍在于,如果您的文件以它们包含的类命名,它立即变得更容易;-)

This is why classes are often named using the PEAR convention : My_Class_Name maps to the file My/Class/Name.php 这就是为什么类通常使用PEAR约定命名的原因: My_Class_Name映射到文件My/Class/Name.php

Your entire statement is correct. 你的整个陈述是正确的。

spl_autoload_register allows you to register multiple autoload functions. spl_autoload_register允许您注册多个自动加载功能。 In the case that you try to create or use an object of a class that has not been loaded into the PHP environment, PHP will run all your autoload functions looking for that class. 如果您尝试创建或使用尚未加载到PHP环境中的类的对象,PHP将运行所有自动加载函数来查找该类。

The Doctrine example you give is using what is called a callback to register a method inside of a class. 您提供的Doctrine示例是使用所谓的回调来在类中注册方法。 A callback is simply an array containing the class name (for static methods) or an instance of that class (for non-static methods), and the method name. 回调只是一个包含类名(对于静态方法)或该类的实例(对于非静态方法)和方法名称的数组。

What say is correct. 说什么是正确的。 It's called when an unknown class is instantiated. 在实例化未知类时调用它。 You then get the chance to include/require the necessary file. 然后,您有机会包含/要求必要的文件。

When the function resides in a class, you have to pass in an array. 当函数驻留在类中时,您必须传入一个数组。 In this case it's a static function because they pass in the Doctrine class instead of an instance of the Doctrine class. 在这种情况下,它是一个静态函数,因为它们传入Doctrine类而不是Doctrine类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM