dongtangu8615 2014-05-04 18:56
浏览 73
已采纳

Yii:autoload($ className)何时被调用?

I'm looking for __autoload magic method inside the YiiBase class, but it isn't. In the official docs said that public static function autoload($className) is

Class autoload loader. This method is provided to be invoked within an __autoload() magic method.

I'm confused. What class of yii framework contains __autoload magic method? Is it possible to get an example with a component class autoloading?

  • 写回答

2条回答 默认 最新

  • dongxiaoke2018 2014-05-04 21:23
    关注

    Yii uses the spl_autoload_register function to set the autoloader function. The spl_autoload_register was introduced in PHP 5.1.2 and makes it possible for us to register multiple autoload functions. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined. By contrast, __autoload() may only be defined once. In Yii you can find this in YiiBase.php, just after the definition of the YiiBase class, at the very end of the file:

    spl_autoload_register(array('YiiBase','autoload'));
    

    Where autoload is a static method definied in the YiiBase class.

    Using spl_autoload_register instead of the __autoload() magic function is good for us, because this way Yii's autoloader does not conflict with our own or with another thrid-party's autoloader. The YiiBase class also defines a registerAutoloader function to register another autoloader function before or after Yii's autoloader in the chain. If you take a look at its code, it is pretty straightforward what it does:

    public static function registerAutoloader($callback, $append=false)
    {
        if($append)
        {
            self::$enableIncludePath=false;
            spl_autoload_register($callback);
        }
        else
        {
            spl_autoload_unregister(array('YiiBase','autoload'));
            spl_autoload_register($callback);
            spl_autoload_register(array('YiiBase','autoload'));
        }
    }
    

    It really just uses spl_autoload_register and/or spl_autoload_unregister (yes, you can also unregister an autoload function!), to put the new autoloader before or after Yii's autoloader, because spl_autoload_register always registers the new autoloader at the end of the chain.

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

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解