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);

Related posts:

  1. Zend Framework – quick tutorial (part 2)
  2. Lambda functions in PHP?
  3. Zend_Validate_Alnum Doesn’t Work Correctly
This entry was posted in zend framework and tagged , , . Bookmark the permalink.

18 Responses to Switch from Zend_Loader to Zend_Loader_Autoloader

  1. Thanks so much.. it’s easier after reading your article and it must be documented in zend framework doc too.

  2. Thanks for this! I’ve just installed Zend for the first time, and I just wanted to get up and running as quick as possible.

  3. Stoimen says:

    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

  4. kkk says:

    actually its enough to write

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

    the ctor of Zend_Loader_Autoloader registers itself [:

  5. Zoltan says:

    Great, worked like a charm.

    Thank you!

  6. I Love PHP says:

    Finally, the answer! Perfect, thanks for sharing this. I bought a book on ZF and its already out of date!

  7. Paul says:

    Thanks. It’s been a while since I last played with ZF and a lot has changed. :)

  8. Abhishek Kumar says:

    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.

  9. Christian Rosenhagen says:

    Great!

  10. Mcartur says:

    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

  11. silas says:

    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.

  12. azdineh says:

    I finally found it Thank you sir!

  13. barbas says:

    @stoimen – very good helped me a lot

  14. Stoimen says:

    @barbas – thanks! glad to help

  15. Lenin says:

    That was a cool solution :D

  16. ali kayahan says:

    $autoloader->setFallbackAutoloader(true);
    setting this solves the issue but we cant use namespaces in this case.

  17. Gildus says:

    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 *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">