duanjia2772 2009-12-14 05:26
浏览 45
已采纳

PHP spl_autoload

i dont really get the docs for 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. 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)?

  • 写回答

3条回答 默认 最新

  • dtsps00544 2009-12-14 05:34
    关注

    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 :

    • 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')

    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?

    Yes, you can, with 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 ;-)

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容