Question

how do i auto load zend framework classes when i am not using the MVC framework?

Was it helpful?

Solution

The nice thing about the Zend framework is that it's extremely modular, you can use just about any piece of it you want without adopting the whole thing.

For example, we can use Zend_Loader_Autoloader to set up class auto-loading without having to use Zend_Application

First make sure the Zend library is in your include path:

set_include_path('/path/to/zend/' . PATH_SEPARATOR . get_include_path());

Then require the Autoloader class:

require_once 'Zend/Loader/Autoloader.php';

Then we set up the autoloader:

// instantiate the loader
$loader = Zend_Loader_Autoloader::getInstance();

// specify class namespaces you want to be auto-loaded.
// 'Zend_' and 'ZendX_' are included by default
$loader->registerNamespace('My_App_');

// optional argument if you want the auto-loader to load ALL namespaces
$loader->setFallbackAutoloader(true);

Once the auto-loader is set up (preferably in a bootstrap or something), you can call Zend framework classes (or your own app's classes) without having to require them individually:

$foo = new Zend_Library_Class();
$bar = new My_App_Class();

Read more about it in the documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top