An extremely useful feature of Zend Framework is the utilization of Namespaces instead of using PHP includes to communicate with your class files. Configure this feature correctly and you have successfully done away with the need to maintain multiple “includes” throughout your app.
If you look at the standard Zend Framework file hierarchy, your controllers reside in “/application->controllers”, and ideally your custom class files reside in “/library->”. To enable all your controllers to communicate with your library files, simply edit your application.ini file like so:
1) Make sure you have your library added to your “includePaths”
includePaths.library = APPLICATION_PATH "/../library"
2) Add this line:
autoloaderNamespaces.Example = Example_
Where “Example” is the Namespace you want to specify.
So now you have the Namespace set-up correctly, how do you use it?
One level directly below your “library” create a directory that echoes your newly created Namespace. e.g. “/library/Example”.
Inside the “Example” directory create a new PHP file named “Service.php”. The naming of your class in “Service.php” should follow the directory structure you have created, so the class name in this case will look like:
class Example_Service {
When trying to create the “Example_Service” object from one of your controllers, you can now call it directly:
public function indexAction()
{
$example = new Example_Service();
//do something
}
There is now no need to have multiple lines of include files to maintain.
Tags: Zend Framework namespace



BROADCAST online marketing


