分享

Using the new autoloaders from Zend Framework...

 sumi2005 2014-07-02

The latest, and last, release of the Zend Framework 1.x series is just around the corner as ZF 1.12.0RC1 was announced this week. As I still have projects running ZF1 I thought about giving the most interesting new feature (for me) a spin - the new autoloaders which are backported from ZF2.

Note: the code below was updated to work with ZF 1.12.0RC2. Should still work with RC1, too.

I decided using the classmap autoloader as the main autoloader, and the good ol' standard autoloader as the fallback autoloader. For the classmap autoloader to work we need to create a classmap. ZF1.12 comes with a tool, located in the bin directory, called classmap_generator.php, which will generate the classmap for us:

  1. cd /path/to/project/library  
  2. $ php /path/to/zf1.12/bin/classmap_generator.php   

This will generate a PHP file called autoload_classmap.php in the library directory and it will have classname - filename mappings of the classes/files from that directory.

Next, need to change the index.php a bit to tell ZF to use the new autoloaders:

  1. // normal setting up of APPLICATION_PATH and other constants here ...  
  2. // Ensure library/ is on include_path  
  3. set_include_path(implode(PATH_SEPARATOR, array(  
  4.     realpath(APPLICATION_PATH . '/../library'),  
  5.     get_include_path(),  
  6. )));  
  7. require_once '../library/Zend/Loader/AutoloaderFactory.php';  
  8. // As of ZF1.12.0RC2 the Zend prefix is not autoregistered  
  9. // with the standard autoloader, so we need to require explicitly  
  10. // the ClassMapAutoloader.php  
  11. require_once '../library/Zend/Loader/ClassMapAutoloader.php';  
  12. Zend_Loader_AutoloaderFactory::factory(  
  13.     array(  
  14.         'Zend_Loader_ClassMapAutoloader' => array(  
  15.             __DIR__ . '/../library/autoload_classmap.php',  
  16.             __DIR__ . '/../application/autoload_classmap.php'  
  17.         ),  
  18.         'Zend_Loader_StandardAutoloader' => array(  
  19.             'prefixes' => array(  
  20.                 'Zend' => __DIR__ . '/../library/Zend'  
  21.             ),  
  22.             'fallback_autoloader' => true  
  23.         )  
  24.     )  
  25. );  
  26. // set up Zend_Application as normal here ...  

and that's about it - the autoloader will load classes from the classmaps, but if it fails to do so, it will fall back to the standard autoloader.

Stripping out require_once calls

The Zend Framework manual has a section on how to improve performance of the framework itself, and one of the suggestion is to strip out the require_once calls from the library. I had to alter that find/sed command combination a bit in order to make it work with the classmaps:

  1. find . -name '*.php' \  
  2.   -not -wholename '*/Application.php' \  
  3.   -not -wholename '*/Loader*' \  
  4.   -print0 | xargs -0 \  
  5.   sed --regexp-extended \  
  6.   --in-place 's/(require_once)/\/\/ \1/g'  

If I'm not wrong in reading my "debugging" echo statements, the standard autoloader gets called only once - to load the classmap autoloader itself - everything else is loaded via the classmaps. Pretty cool.

Happy hackin'!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章