• Home
  • About
  • Links
  • Contact
  • Imprint
Blue Orange Green Pink Purple

The blessings of an Autoloader

Posted in php. on Sunday, November 15th, 2009 by Chris Tags: php, zend, zendFramework
Nov 15

Lately I came across the nice little autoloader provided by the Zend Framework. So I like to share some experiences. First of all, like I mentioned in the “About” section of my page, to me Zend is more like a new PEAR, but not like a framework to build websites. This is why I use it together with some other php frameworks and CM-Systems.

But back to the topic of this post,.. I came across the new Autoloader Class provided with the Zend Framework, studied some of the documentation. And Failed,.. perhaps I over-read something but where does the documentation mention that your class has to be prefixed with the name of the namespace one registers ?

But one by one, if you like to use the autoloader either with your extension to the CM-System of your choice (in my case this would be Typo3 at 90% of the projects I work on), or within some php-code you write for a customer, you will have to start wit something like this:

  set_include_path ( '.' . PATH_SEPARATOR . 'path/to/Zend_Framework/' );

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

This was how far I got with the documentation. Because I know some other languages like JAVA, I knew that I would have to create a nice little sub folder called “nameOfYourNameSpace” in the directory for a SOAP – Client I lately wrote for a customer of mine. I went on creating the class file I needed:

  class myClass
  {
     public function __construct()
     {
        // here the code Starts
     }
  }

Like I said I am used to the conventions of other languages when it comes to namspaces, so I called the file myClass.php. Now in my imprudence I thought something like this would work right from the Start, as well as I am getting rid of those require_once statements:

  $o_myObject = new nameOfYourNameSpace_myClass();

Hhmmm,… nope it doesen`t,… turns out that the class within the file has to be prefixed by the name of the namespace it self. So I had to change the php class to this:

  class nameOfYourNameSpace_myClass
  {
     public function __construct()
     {
        // here the code Starts
     }
  }

After that, the first code segment worked fine for me, and I had a nice little autoloader to load my models. Is it just me, or would you too have expected that it should be enough to create the folder structure registered in the first snippet and call the class just like the file name ?

Perhaps this was a little help for you getting to know the Zend_Loader_Autoloader.
Drop me some thoughts if you like.

Leave a Reply

SwiftLizard Interactive {Design, Development}

  • Last Posts
    • Scrum Master
    • Loging into a page on shell with curl
    • Developing a clock in Flash with AS3
    • Javascript leashed with google Caja
    • Sometimes I wonder about Developers
  • Categories
    • css (1)
    • design (2)
    • development (2)
    • javascript (4)
    • jquery (3)
    • php (4)
    • project management (2)
    • scrum (2)
    • shell (1)
    • thoughts (7)
    • typo3 (3)
    • xhtml (1)
    • Zend Framework (1)
  • Tags
    actionscript 3.0 agile agile development caja continuous integration css design development Flash Flashbuilder javascript jquery Lean OpenAgile performance optimization php product design qunit scrum security shell sql thoughts typo3 xhtml zend zendFramework
  • Friends Blogs
    • Dmitry Dulepov Blog
    • Dominique Stender
    • Gina Steiners Blog
    • Iphone Fan by Dirk Kunde
    • Mario Rimann Blog
    • Michi Zepernick
    • Thomas Hempel :: Typo3 Unleashed
  • Archives
    • March 2010
    • December 2009
    • November 2009
  • Search


  • t3n Social News bookmark at mister wong publish in twitter bookmark at del.icio.us bookmark at digg.com bookmark at furl.net bookmark at linksilo.de bookmark at reddit.com bookmark at spurl.net bookmark at technorati.com bookmark at google.com bookmark at yahoo.com bookmark at facebook.com bookmark at stumbleupon.com bookmark at propeller.com bookmark at newsvine.com bookmark at jumptags.com

RSS Feed


  • Home
  • About
  • Links
  • Contact
  • Imprint

© Copyright SwiftLizard Interactive {Design, Development}. All rights reserved.
Designed by FTL Wordpress Themes brought to you by Smashing Magazine

Back to Top