简体   繁体   English

禁用Zend自动加载器

[英]Disable Zend Autoloader

如何禁用Zend_Loader_Autoloader?

You could manually force the Autoloader to unload, but this may lead to trouble with components depending on it being registered: make sure your other loader covers that. 您可以手动强制卸载自动加载器,但这可能会导致组件故障,具体取决于已注册的组件:请确保其他加载器能够解决该问题。

spl_autoload_unregister(array('Zend_Loader_Autoloader','autoload'));

I stripped this from the constructor of Zend_Loader_Autoloader, and changed it to work outside of the class, and to unregister instead of register the loader. 我从Zend_Loader_Autoloader的构造函数中删除了它,并将其更改为在类之外工作,并取消注册而不是注册加载程序。

If you're using the Zend_Application, in your index.php, after creating the instance of ZA, you can get/set the autoloader you want ZF to use: 如果使用的是Zend_Application,则在创建ZA实例之后,在index.php中,可以获取/设置要ZF使用的自动加载器:

$app = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/config.ini');
$app->getAutoloader()->setDefaultAutoloader('yourAutoloader');
$app->bootstrap()->run();

HTH 高温超导

What about in your bootstrap.php 那在bootstrap.php中呢

protected function _initAutoloader()
{

       $this->getApplication()
            ->getAutoLoader()
            ->unregisterNamespace("Zend");

       // or 
       $this->getApplication()
            ->getAutoloader()
            ->removeAutoloader();
}

I assume you're using Zend_Application , which automatically sets up the PHP environment, autoloading and bootstrapping. 我假设您正在使用Zend_Application ,它会自动设置PHP环境,自动加载和引导。 It's very handy. 非常方便。 Sadly, setting up Zend_Autoloader is hard coded into the constructor, and I can't see any way to override it: 可悲的是,设置Zend_Autoloader是硬编码到构造函数中的,我看不到任何方法可以覆盖它:

public function __construct($environment, $options = null)
{
    $this->_environment = (string) $environment;

    require_once 'Zend/Loader/Autoloader.php';
    $this->_autoloader = Zend_Loader_Autoloader::getInstance();

    //snip
}

My first suggestion would be to find a way to make Zend_Autoloader and your other autoloader work in harmony. 我的第一个建议是找到一种使Zend_Autoloader和您的其他自动加载器协调工作的方法。 I've been using Zend_Autoloader with the new Doctrine::IsolatedClassLoader with no problems. 我一直在使用Zend_Autoloader和新的Doctrine::IsolatedClassLoader没问题。 The advice about being explicit about autoloader namespaces or using pushAutoloader() is valid, and should work. 关于显式地显示有关自动加载器名称空间或使用pushAutoloader()是有效的,并且应该起作用。

However, if that is not an option, you should probably abandon using Zend_Application , and handle environment setup and bootstrapping yourself. 但是,如果这不是一个选择,则您可能应该放弃使用Zend_Application ,并处理环境设置并自行引导。 While an inconvenience, it's shouldn't be too difficult. 尽管带来了不便,但应该不会太困难。 Most ZF tutorials prior to version 1.8 (which is when Zend_Application was introduced) provided examples. 版本1.8之前的大多数ZF教程(引入Zend_Application时)都提供了示例。

Here's a (now outdated) set of slides detailing some of this: 这是一组(现在已过时)的幻灯片,详细介绍了其中的一些内容:

Getting Started With Zend Framework for v1.6 Zend Framework v1.6 入门

Zend_Loader::registerAutoload('Zend_Loader', false); Zend_Loader :: registerAutoload('Zend_Loader',false);

Its in the framework documentation http://framework.zend.com/manual/en/zend.loader.html#zend.loader.load.autoload 它在框架文档中http://framework.zend.com/manual/en/zend.loader.html#zend.loader.load.autoload

However, I don't think you should have any problems leaving the zend autoloader enabled as long as you register your autoload callback using spl_autoload_register() so it gets added to the autoload stack. 但是,我认为只要使用spl_autoload_register()注册自动加载回调以将其添加到自动加载堆栈中,只要启用zend自动加载器就不会有任何问题。

Using spl_autoload_register, all callbacks will be called to try to satisfy the request. 使用spl_autoload_register,将调用所有回调以尝试满足请求。 I don't know if the chain will be interrupted once a callback is successful. 我不知道一旦回调成功,链是否会中断。 If it does then its probably controlled by returning true or false in the callbacks. 如果这样做,则可能通过在回调中返回true或false来控制它。

I'd suggest using "push autoloader" method of Autoloader, which can recieve other autoloader instance and call it when Namespaces match. 我建议使用自动加载器的“ push autoloader”方法,该方法可以接收其他自动加载器实例,并在命名空间匹配时调用它。 Therefore you can use your autoloader along with the zend's one... 因此,您可以将自动装带器与zend的自动装带器一起使用...

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

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