Zend_Loader's autoloader deprecated in Zend Framework 1.8

Zend_Loader's autoloader has been deprecated in the upcoming Zend Framework version 1.8 and so you now get a notice if you use it:

Notice: Zend_Loader::Zend_Loader::registerAutoload is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead in /www/zf-tutorial/library/Zend/Loader.php on line 207

Notice: Zend_Loader::Zend_Loader::autoload is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead in /www/zf-tutorial/library/Zend/Loader.php on line 186

Notice: Zend_Loader::Zend_Loader::autoload is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead in /www/zf-tutorial/library/Zend/Loader.php on line 186

(and so on)

This is because you have the lines:


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

(or similar) somewhere in your bootstrap system.

The easiest solution is to change them to:


require_once 'Zend/Loader/Autoloader.php';
$loader Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('App_');

Where 'App_' is the name of a directory on your include path that has classes within it that follow the Zend Framework naming convention, so change it as appropriate and add more if you need them.

If you need generic autoloading ability, for instance because your models directory is on your include path and you don't namespace your model classes, then add this:


$loader->setFallbackAutoloader(true);

You should also add:


$loader->suppressNotFoundWarnings(false);

when in development mode as then the new autoloader will actually tell you what the syntax error is rather than showing you a white page :)

Now you should go and read the Zend_Loader_Autoloader documentation to learn how very useful it is!

Update: You should also read Matthew's article on Zend_Loader_Autoloader as it explains this all in detail!

25 Responses to “Zend_Loader's autoloader deprecated in Zend Framework 1.8”

  1. 1 Jan

    Your title is a bit misleading, not the whole Zend_Loader is deprecated but the autoloading facilities.

    Zend_Loader::loadClass() for example is used by Zend_Loader_Autoloader.

  2. 2 Rob...

    Jan,

    You are right. I have fixed the title.

    Regards,

    Rob...

  3. 3 Gerard

    I liked

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

    it's simple, two lines, no messing around :)

  4. 4 stefano

    Thanks a lot, you save my time!!
    stefano

  5. 5 mvug

    hey guys,

    this new method doesn't seem to work with my code... I used the autoloader just to autload zend_pdf (i replaced all require_once references and the autoloader requires automaticly the needed zend_pdf files..) can anybody help me with that? this is my old code:

  6. 6 mvug

    my code:

    // LOAD ZEND_PDF
    $path = '../system/plugins';
    set_include_path( get_include_path().PATH_SEPARATOR.$path );
    require_once $path.'/Zend/Loader.php';
    Zend_Loader::registerAutoload();

    Zend_Pdf::load('test.pdf');

  7. 7 KingCrunch

    @mvug
    As mentioned above Zend_Loader::registerAutoload() is deprecated. So I dont think its useful to give hints for this feature since now ;)

  8. 8 Justice

    Hi Rob,

    thanks for your advice.

  9. 9 benr

    When you say:
    'If you need generic autoloading ability, for instance because your models directory is on your include path and you don't namespace your model classes, then add this:'

    What do you mean by namespacing model classes? What is the best practice here?

    Thanks.

  10. 10 Rob...

    benr,

    Best practice for an article model in a CMS module would be to call it Cms_Model_Article or similar. This would make it less likely to conflict with another class when moving your CMS module to a different app.

    Regards,

    Rob...

  11. 11 Ben

    Maybe im an idiot, but I cant seem to get this working...

    My code looks like the following (note I have uncommented and commented different portions unsuccessfully)

    require_once "Zend/Loader/Autoloader.php";
    //require_once "doctrine/Doctrine.php";

    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->registerNamespace('Wfm_');
    $autoloader->setFallbackAutoloader(true);

    //$autoloader->pushAutoloader(array('Doctrine', 'autoload'), 'Doctrine');

    I have a folder in my /library called Wfm with a number of classes, however, it fatals out on the first inclusion of a Wfm_ prefixed file. The path looks like...

    /library/Wfm/Layout/Controller/Plugin/Layout.php - However, it complains that it cannot find the file, or the class does not exist within the file. My PHP include path includes the library folder - am I just missing something obvious here?

  12. 12 Rob...

    Ben,

    Assuming the class is called Wfm_Layout_Controller_Plugin_Layout within layout.php, I can't see anything obvious. Sorry.

    I'd double check the output of get_include_path() too.

    Regards,

    Rob...

  13. 13 Ben

    Rob,

    Thanks for the quick response. Unless ive gone absolutely mad, im 100% sure that the file is there. I copied the entire Wfm folder from another project where it worked without issue.

    It seems, at this point, to only be an issue with that file, as odd as that sounds, if I remove the line in the bootstrap:

    Zend_Layout::startMvc(array(
    'layoutPath' => APP_DIR . DS . 'layouts',
    'pluginClass' => 'Wfm_Layout_Controller_Plugin_Layout'
    ));

    Then it loads up saying it cant find the index controller which makes sense because its a new project - it doesnt fatal out, so I assume its working.

    The include path looks correct, here is the first portion of it... Warning: include() [function.include]: Failed opening 'Wfm\Layout\Controller\Plugin\Layout.php' for inclusion (include_path='C:\Workspace\Projects\XXXXXX\site\library;

    Excluding other include path's - that one right there should cover it assuming the file is in C:\Workspace\Projects\XXXXXX\site\library\Wfm\Layout\Controller\Plugin\Layout.php

    I guess the last thing I can say is that this entire config, file structure, etc works on pre 1.8 applications without fail.

  14. 14 Ben

    I take that back.. anything under the Wfm_ namespace fails miserably..

  15. 15 sunil

    hi
    setting probleam accured
    why

  16. 16 Adrian

    Hi,
    Thanks for your tutorial. Found it really helpful.
    It's a shame the ZF manual doesn't explain this in such a simple, straight forward way.

  17. 17 GP

    Hey Rob, thanks for your great article (again) :)

    I upgraded to 1.8.3 and got the above mentioned error message.

    Am I right when this:
    include_once('Zend/Loader/Autoloader.php');
    $loader = Zend_Loader_Autoloader::getInstance();
    $loader->setFallbackAutoloader(true);
    $loader->suppressNotFoundWarnings(false);

    Is the same as the previously used:
    include_once('Zend/Loader.php');
    Zend_Loader::registerAutoload();

    It appears to be that way in my current app.

  18. 18 Rob...

    GP,

    Yes. I suspect that suppressNotFoundWarnings may be set to false by default nowadays, but it never hurts to be explicit.

    Regards,

    Rob...

  19. 19 guelmismr

    Recently I started to get familiar with the MVC and I was recomended to work with Zend Framework, but it has taken me several days trying to configure the Boostrap.php, the .htacces and index.php on public folder, so far it works for the IndexController and ErrorController, but when adding new controllers is not very clear how should I call them, because I can not even define who is the front controller, I need a basic setup to begin to develop an application, the base. Could someone help me?

  20. 20 apprentice

    guelmismr>>>
    http://akrabat.com/zend-framework-tutorial/
    Try the new 1.8 version of Rob's Tutorial it looks fresh and easy, will begin to poke holes on my punch cards tomorrow before running the codes :-)

    By the way guelmismr it has taken me around 360 days (~12 months) to configure LAMP/WAMP, Bootstrapper, .htaccess, index.php, reading the basic chapters of ZFiA etc. etc.

    I still haven't been able to play with the HTML and CSS part yet, maybe tomorrow when my punch cards are hot and ready :-)

  21. 21 Michele

    thank's a lot, i found this really useful!

  22. 22 Dave Russell

    I am currently redoing my website using zend framework and chose your book. The zf version is 1.9.7 . Page 71...The baseUrl View Helper. The getBaseUrl() now includes index.php in 1.9.7...so you need to strip it out in the view helper:
    i.e.
    class Places_View_Helper_BaseUrl
    {
    function baseUrl()
    {
    $fc = Zend_Controller_Front::getInstance();
    $baseUrlWithIndex = $fc->getBaseUrl();

    $base_url = substr($_SERVER['PHP_SELF'], 0, -9);
    return $this->_baseUrl = $base_url;
    }
    }

  23. 23 Dave

    An adjustment to my comment. You can't just strip out index.php. You need to set the baseUrl.

  24. 24 Frank

    Using ZF 1.10.1 and Zend_Tool.

    Listing 2.1 differs greatly from the index.php file that Zend_Tool places in the public folder.

    Have things changed that much since ZF 1.5? Can I still use this book?

  25. 25 Rob...

    Frank,

    Yes the book is still useful for ZF development. However, the MVC chapters, (2, 3 & 4) are less useful nowadays. Essentially, you need to go through my tutorial and then look at how 2,3,4 differ.

    Regards,

    Rob...

The views expressed in these comments are not the views of the publisher. However, we believe in the rights of others to express their legitimate views and concerns. Any legitimate complaint emailed to rob@akrabat.com will be seriously considered and the post reviewed as desirable and necessary.

Leave a Reply

Buy now!