Loading custom classes from the library directory in Zend

2.4k Views Asked by At

I'm trying to load a whole set of classes from a namespace. These are just utility classes, and I don't want to treat them as action helpers, view helpers, or any sort of plugin (I have those working great thanks to the docs).

I have the Zend-like directory structure going, e.g., a class called Resources_Employee_Salary is stored under library/Resources/Employee/Salary.php.

Now when I'm in my action controller, I want to create an instance of Resources_Employee_Salary, but I can't because it can't find the class.

What do I have to do to get Zend to load the classes under Resources/* once and for all? I looked at the Zend_Loader but that has methods which ask for a particular file or a class, I want to load the whole directory.

Any help will be appreciated.

Thank you,

1

There are 1 best solutions below

3
On BEST ANSWER

You need to name your classes appropriately...

Path: library/Resources/Employee/Salary.php
Class: Resources_Employee_Salary

Then you need to register your namespace with the autoloader...

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Resources_');

Of course you can also specify this in your projects configuration file so you dont have to do it manually... In XML format this would look like (not sure about ini format if thats what you like...):

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
  <production>
    <autoloaderNamespaces>
      <resources value="Resources_" />
    </autoloaderNamespaces>
  </production>
</application>