3

I have a singleton class in my models directory and I have to use its function in Controller class. Doing it by require_once('file path'); and calling function as ClassName::FunctionName() works fine, but I need to use Zend Autoloader instead of including class through require_once. I came across number of solutions here on stackoverflow which used bootstrap.php in terms of adding following code there and it seems like doing the same as require_once('file path'); did in controller

  protected function _initAutoload()
    {   
       Zend_Loader_Autoloader::getInstance();
    }

Going this way I get Fatal error: Class 'ClassName' not found in {path}\controllers\SampleController.php on {line no.} I am sure I am missing something but cant figure out what exactly.

Sidra Sultana
  • 529
  • 1
  • 5
  • 20

3 Answers3

2

Like user1145086 has rightly said, if you follow the naming convention of Zend, you class should be auto-loaded.

If you have a class, say AutoloadedClass, and you want it auto-loaded, you can do the following:

  1. Create a folder in your /library folder and name it 'My'.
  2. Write the following code in your Bootstrap's initAutoload class method:

    Zend_Loader_Autoloader::getInstance()->registerNamespace(array('My_'));
    
  3. Place the file containing the 'AutoloadedClass' in the 'My' folder you just created and rename the file to AutoloadedClass.php so the file is eventually located thus: /library/My/AutoloadedClass.php
  4. Lastly, rename the class itself to My_AutoloadedClass as Zend's naming convention requires. You can henceforth get a reference to your class using that class name My_AutoloadedClass from anywhere in your application.
burntblark
  • 1,680
  • 1
  • 15
  • 25
  • 1
    Or subclass the problematic class and put a `require_once` at the top: `require_once 'SomeClassWithProblematicFilename.php'; class My_AutloadedClass extends TheProblematicClass {} ` – David Weinraub Jan 13 '12 at 08:39
  • in this way i have to add some Prefix (My_ in this case) to my class. Is there any way to autoload a class with name ,for instance, SampleClass (having no prefix) – Sidra Sultana Jan 13 '12 at 10:52
  • 1
    You have to put your classes (with the proper naming convention) in a particular registered namespace (just like 'My') for it to be loaded automatically. Otherwise, you would have to either write your own autoloader class or use require/include_once. – burntblark Jan 13 '12 at 11:18
  • got it, i managed to change the Class name as per prefix. thanks alot – Sidra Sultana Jan 13 '12 at 11:44
1

if you name your class according to zend conventions, the class should autoload without problem. If your class is located at /application/models/myClass.php and is named :

class Application_Model_MyClass {

    public function myMethod(){
    }
}

it should autoload just fine, I don't think the fact that is a singleton would affect autoloading.

If you need to use your own class names you can add a new namespace to the autoloader the works in the /library directory, add this line to your application.ini :

autoloaderNamespaces[] = "MyNamespace_"

then add the /MyNamespace directory to the /library directory and name your files accordingly:

class MyNamespace_MyClass {
}

Hope this helps.

RockyFord
  • 8,529
  • 1
  • 15
  • 21
  • Thanks for the help. Class to be loaded is not a model class. I cant add any prefix before class name. – Sidra Sultana Jan 13 '12 at 07:26
  • @sidra can you tell us why? Maybe a bit more detail of your actual problem should be added to your question. – vascowhite Jan 13 '12 at 08:10
  • ya sure. I have a FileUploader class, having addFile function. This file is residing there in models directory and I cant change its name but can change its path i.e. can place it in library folder. Now i have to use FileUploader::addFile() by autoloading this class there in controller. Any help will be appreciated – Sidra Sultana Jan 13 '12 at 11:13
1

If class names and locations in your library do not follow Zend naming conventions, then you can write an autoloader for that library and push this autoloader onto the Zend_Loader_Autoloader stack.

See https://stackoverflow.com/a/8820536/131824 for an example.

Community
  • 1
  • 1
David Weinraub
  • 14,144
  • 4
  • 42
  • 64
  • in this way i have to add some Prefix (My_Loader_Autoloader_ in this case) to my class. Is there any way to autoload a class with name ,for instance, SampleClass (having no prefix) – Sidra Sultana Jan 13 '12 at 10:53
  • 1
    @SidraSultana: No, I think you are misunderstanding. No prefix necessary. Your custom autoloader - like the one in the PHPExcel example linked in the above answer - is pushed it onto the Zend autoloader stack. It implements a method `public function autoload($class)` that checks the desired `$class` against the names of the classes that need some special include/require handling. If no match, return false (quickly, so the other autoloaders in the stack get their chance). Otherwise, perform your specialized include/require to load the class and return true. – David Weinraub Jan 13 '12 at 13:39
  • ya you are right. I mis-interpret My_Loader_Autoloader_PHPExcel as a class to be required in the controller whereas PHPExcel is the required class. Thanks alot David Weinraub – Sidra Sultana Jan 14 '12 at 14:51