views:

479

answers:

6

How can I disable the Zend_Loader_Autoloader?

+1  A: 

What about in your bootstrap.php

protected function _initAutoloader()
{

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

       // or 
       $this->getApplication()
            ->getAutoloader()
            ->removeAutoloader();
}
Travis
Does not work, this is a deprecated function call.
powtac
I updated the answer with code that should conform to the newer Zend_Loader_Autoload API.
Travis
BTW, Formatting is a key ingredient to every answer.
Chacha102
A: 

I'd suggest using "push autoloader" method of Autoloader, which can recieve other autoloader instance and call it when Namespaces match. Therefore you can use your autoloader along with the zend's one...

Tomáš Fejfar
I dont want to use both, I checked this option already. I really want to disable the ZF Autoloader.
powtac
Then just don't use it (=don't call require_once). It won't be used anywhere else.
Tomáš Fejfar
@tomas.fejfar The autoloader is called when you DON'T call require_one. besides, his question is removing the loader to replace it.
The Guy Of Doom
No, it's the only class needed to be explicitly required by `require_once 'Zend/Loader/Autoloader.php;`
Tomáš Fejfar
+1  A: 

I assume you're using Zend_Application, which automatically sets up the PHP environment, autoloading and bootstrapping. 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:

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. I've been using Zend_Autoloader with the new Doctrine::IsolatedClassLoader with no problems. The advice about being explicit about autoloader namespaces or using pushAutoloader() is valid, and should work.

However, if that is not an option, you should probably abandon using Zend_Application, and handle environment setup and bootstrapping yourself. 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.

Here's a (now outdated) set of slides detailing some of this:

Getting Started With Zend Framework for v1.6

Bryan M.
What about extending Zend_Application and overriding the constructor?
Karsten
It's possible, but you'd need to account for any other code calling getAutoloader() on the application object's instance. Or pass in your own loader object that's 100% compatible with the zend autoloader.
Bryan M.
+1  A: 

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.

The Guy Of Doom
I will try this solution. I think it fits best. I don't want to use the Z_Autoloader::unload() stuff, it seems not to work propably.
powtac
I have to insert `spl_autoload_unregister(array('Zend_Loader_Autoloader', 'autoload'));` just before `$application->bootstrap()->run();`. And I have to register my own autoload by `spl_autoload_register('__autoload');` This seems to work.
powtac
+2  A: 

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:

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

HTH

robertbasic
+1  A: 

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

Its in the framework documentation 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.

Using spl_autoload_register, all callbacks will be called to try to satisfy the request. 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.

spatel