Switch from Zend_Loader to Zend_Loader_Autoloader

Zend_Loader

Zend_Loader was the usual kind of autoloading in Zend Framework before version 1.8. Than simply you say:

require_once “Zend/Loader.php”;
Zend_Loader::registerAutoload();

That made all your application to autoload files from the library folder where usualy the Zend Framework stays. That has several things to be changed.

The namespaces

There where not really namespaces for autoloading. Everything was searched to be loaded in one single namespace. And thus there where not the functionality to remove a namespace from the game. You have everything all the time.

Zend_Loader_Autoloader

This is new from version 1.8 in Zend Framework. Now you should use this class to autoload your classes. There’s really namespaces and for more info and detailed information you should search the documentation of the ZF.

The most simple way to switch from Zend_Loader to Zend_Loader_Autoloader

That’s really simple, and I don’t understand why there’s not proper explanation how to do that in the ZF docs. You need to replaces this lines of code:

require_once “Zend/Loader.php”;
Zend_Loader::registerAutoload();

with these:

require_once ‘Zend/Loader/Autoloader.php’;
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

18 thoughts on “Switch from Zend_Loader to Zend_Loader_Autoloader

  1. Hi Michael,

    it’s great I have helped you! I’d be glad to share my experience with Zend Framework if you have more questions.

    greetings

  2. actually its enough to write

    require_once ‘Zend/Loader/Autoloader.php’;
    Zend_Loader_Autoloader::getInstance();

    the ctor of Zend_Loader_Autoloader registers itself [:

  3. kkk says that this line is not necessary

    $autoloader->setFallbackAutoloader(true);

    But if we dont use it, there will be an error. I got this error:

    Fatal error: Class ‘Templater’ not found in C:\xampp\htdocs\phpweb20\htdocs\index.php on line 36

    The line 36 of my code is

    $vr->setView(new Templater());

    When I included all three lines of code:

    require_once (‘Zend/Loader/Autoloader.php’);
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->setFallbackAutoloader(true);

    the error vanished.

  4. Thank you very much, I was wondering how to get use of the different classes of zendframework without setting up an application¡¡ It was worthy ¡¡ Thanks

  5. hi, Abhishek Kumar,
    I just reading the same book as yours. the samethings happened like yours.
    chinese words are “,同是天涯沦落人,相逢何必曾相识”, that says we meets same things.

  6. The example not apply when you use some clases with parameters.

    Ah!, And for be more exactly you must paste these code into bootstrap.

    😉

    Gildus

Leave a Reply

Your email address will not be published. Required fields are marked *