3

I developing a project using Zend framework and I came across the following problem. I'm using Zend framework MVC folder structure generated using their zf.sh script.

My library folder has the Zend library folder and it's classes can be called normally inside the application. I created another folder inside my library for my classes. This is the folder structure now:

MyProject

|_application
|_docs
|_public
|_library
           |_Zend
           |_Buyers
                     |_Donations.php
|_scripts

I named my Donation class "Buyers_Donations" as the Zend framework naming convention.

When I tried using this class inside my controller

$obj= new Buyers_Donation(); 

it gave an error can not find class Buyers_Donation inside the controller.

But when I added the following line in my Bootstrap it worked:

$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
$moduleLoder = new Zend_Application_Module_Autoloader(
array(
'namespace'=>'',
'basePath'=>dirname(__FILE__)
));

Could someone please explain what actually happened and what is the use of the module autoloader although I don't have any modules in my application ?

Songo
  • 5,618
  • 8
  • 58
  • 96

2 Answers2

5

As you suspected, you shouldn't be using the module autoloader since you're not using modules. Assuming the Zend* classes are autoloading correctly for you, all you need to do is tell the standard autoloader that it should also be used for classes in your 'Buyers' namespace. So instead of the code snippet you posted, just do:

$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Buyers_');

you can also set this in application.ini if you prefer.

I'm also assuming that your classes are in the library folder, and not in the public directory as your question implies (this would be bad).

Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
  • Thanks for your reply. Can you explain please what exactly does the function "setFallbackAutoloader(true);" do and compare it to "registerNamespace('Buyers_');" ? and another question why aren't the classes loaded although the pass to the library is on the include path ? – Songo Jun 23 '11 at 11:55
  • 1
    registerNamespace('Buyers_') tells ZF that that autoloader should be used for any classes starting with 'Buyers_'. setFallbackAutoloader(true) tells ZF that that autoloader should be used for any classes not matched by another autoloader. – Tim Fountain Jun 23 '11 at 12:04
  • hmmm I get it now, but will there be a problem to use setFallbackAutoloader(true) instead of registerNamespace() ? overhead maybe ? and one last things why are the zend classes loaded by default ? where is it written or mentioned ? – Songo Jun 23 '11 at 12:09
  • The standard autoloader registers the namespace of Zend_ automatically. I would suggest using `registerNamespace('Buyers_')` just to avoid problems later on. The fallback option is really only for libraries which don't have a common namespace. – Tim Fountain Jun 23 '11 at 12:21
1

If you do not wish to use the zend's auto loading feature, you will have to include files manually by using require_once(), such as:

 require_once 'Buyer/Donations.php';

If you do wish to use zend loader with your own library code that uses your own namespace, you may register it with the autoloader using the registerNamespace() method. in the bootstrap, you could do so as follows:

protected function _initAutoload()
{

    $autoloader = Zend_Loader_Autoloader::getInstance()->
                           registerNamespace('Buyers_')
    return $autoloader;
}

If the auto loader doesn't work, make sure you set the include path to the library folder somewhere. It's automatically added by the zend framework to public/index.php:

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
aporat
  • 5,922
  • 5
  • 32
  • 54
  • Thanks for the reply. So basically what you mean is that the autoloader "requires once" the file which contains the class I specify ? – Songo Jun 23 '11 at 11:57