0

Due to performance issues (benchmarked) I'm trying to use another autoloader than the default Zend_Loader_Autoloader.

I tried to different method, using :

$autoloader->removeAutoloader(array('Zend_Loader_Autoloader', 'autoload'))
    ->setDefaultAutoloader(array($loader, 'loadClass'));

Since the Autoloader is heavily linked to most component of Zend Framework I can't "remove it".

I did an Xdebug step by step debug and it looks like the Autoloader works like this :

1) getInstance 2) Check if the called class root is a known namespace and if an autoloader exists for its namespace 3) Put on the stack the non namespaced autoloader 4) Tried autoloader on each autoloader, until a valid in found.

However, in my case my autoloader already do this (I'm using Opl Autoloader with a classMap strategy), I allready register the namspace with their respective path.

So it looks a bit overhead for me because I'd like my autoloader be used at the very begging of the lookup bypassing all Zend checks.

Do you have any ideas on improving that? How could I efficiently use a custom (and performant) autoloader within a Zend Framework project and using Zend_Loader_Autoloader

Trent
  • 5,785
  • 6
  • 32
  • 43
  • @mario, it's what I actually do, OPL provides a ClassMapLoader and its classMap builder which generates an array like "array('My_Class_Name' => 'My/Class/Name.php'); which is pretty straightforward – Trent Jan 02 '12 at 13:34

2 Answers2

1

The autoloader isn't "heavily linked". You can always use every PSR-0 complaint autoloader you like, but in this case you should not use Zend_Loader_Autoloader to register the autoloader into Zend_Loader_Autoloader to replace Zend_Loader_Autoloader ;)

spl_autoload_register($myAutoload);
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • Well ZF is linked with Zend_Loader & Zend_Loader_Autoloader, some classes (IE Zend_Mail) use Zend_Loader::loadClass() method which actually uses Zend_Loader_Autoloader, as far as I know Zend_Loader_Autoloader is just a wrapper to spl_autoloader_register, but since it tends to register itself automaticly while using Zend_Application (which **needs** Zend_Loader_Autoloader). However, do you think that I could go to the top of the stack of the autoloader if I register mine early before all the ZF bootstrap? My goal is to never use ZF autoloader, except as a fallback. – Trent Jan 02 '12 at 13:37
  • Didn't notice, that some components call `loadClass()` directly ... yes, you can register as much autoloaders as you like with `spl_autoload_register()` and as soon as the class exists PHP will break the autoloading chain, meaning: If your loader loads the class, no further loader is invoked. – KingCrunch Jan 02 '12 at 13:48
  • ok, I'll give it a try. (Take a look at library/Zend/Mail/Transport/Smtp.php: Zend_Loader::loadClass($connectionClass); ) – Trent Jan 02 '12 at 13:59
1

While not answer by itself this post from the Zend lead, Mathew, might prove helpful in demonstrating how to drop in a replacement autoloader (and a more efficient one at that)

http://mwop.net/blog/262-Backported-ZF2-Autoloaders

James Butler
  • 3,852
  • 1
  • 26
  • 38