1
votes

What is best possible approach to autoload classes from a PHP Project which is residing in

PHP Project

C:
|Projects
         |MyProject
                   |Classes
                           |CStudents.php
                                          |Student (ClassName)
                                                   |getStudentName() (Method)

All the classes have prefixes, such that, if the class is Students then it resides in CStudents.php

Right now I need to access these classes pull out some data and further with the Zend Framework 2 modules.

Zend Framework 2 Project:

C:
|Projects
         |Zend
              |module
                     |Application
                                 |Module.php

So what would be the simple and best possible approach for this. I need to autoload not more than 50 Classes.

EDIT: My question is almost similar to this one, but the only difference is that I have the requirement for Zend Framework 2.

As mentioned all Classes are residing in Classes folder, Class files have "C" [CStudent.php] as prefixes and the class names [Class Student]start without the prefixes, so lets say I want to access Student::getStudentName(); How can I accomplish that?

Thanks

1
check the docs for esage of prefixes and namespaces: framework.zend.com/manual/2.0/en/modules/…Andrew

1 Answers

0
votes

Here is an example of how I use the Autoloader.

Module.php

/**
 * Autoloader
 *
 * @return array
 */
public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php'
        )
    );
}

ZF2 comes with a classmap_generator.php which will build the required PHP Array of all the classes found in your Module. You can run classmap_generator -l modules/NameOfYourModule.

This will output a file called autoload_classmap.php and place it inside of your Module/NameOfYourModule folder for you.

You can also find a lot more information in the documentation http://zf2.readthedocs.org/en/latest/modules/zend.loader.autoloader-factory.html