It can be useful to add repositories of user defined classes to the eZ
components autoload system. The ezcBase::addClassRepository() method can be
used to perform this task. At you need to arrange the desired external classes
in a class repository, i.e. make sure that classes and correspondent
*_autoload.php files are named and placed accordingly to explanations below.
After they are in the proper structure you can call addClassRepository() with
the proper parameters somewhere in code before you use the external classes.
External classes will then be loaded by autoload system.
ezcBase::addClassRepository() takes two arguments:
- $basePath is the base path for the whole class repository.
- $autoloadDirPath the path where autoload files for this repository are found.
The paths in the autoload files are not relative to the package directory
as specified by the $basePath argument. I.e. class definition files will
be searched for in the location $autoloadDirPath only.
Example:
Take the following facts:
- There is a class repository stored in the directory "./repos".
- Autoload files for that repository are stored in "./repos/autoloads".
- There are two components in this repository: "Me" and "You".
- The "Me" component has the classes "erMyClass1" and "erMyClass2".
- The "You" component has the classes "erYourClass1" and "erYourClass2".
In this case you need to create the following files in "./repos/autoloads".
Please note that the part before _autoload.php in the filename is the first
part of the classname, not considering the all lower-case letter prefix.
Content of my_autoload.php:
1. <?php
2. return array (
3. 'erMyClass1' => 'Me/myclass1.php',
4. 'erMyClass2' => 'Me/myclass2.php',
5. );
6. ?>
Content of your_autoload.php:
1. <?php
2. return array (
3. 'erYourClass1' => 'You/yourclass1.php',
4. 'erYourClass2' => 'You/yourclass2.php',
5. );
6. ?>
The directory structure for the external repository is then:
./repos/autoloads/my_autoload.php
./repos/autoloads/your_autoload.php
./repos/Me/myclass1.php
./repos/Me/myclass2.php
./repos/You/yourclass1.php
./repos/You/yourclass2.php
To use this repository with the autoload mechanism you have to use the
following code:
1. <?php
2. require_once 'tutorial_autoload.php';
3.
4. ezcBase::addClassRepository( './repos', './repos/autoloads' );
5. $myVar1 = new erMyClass2();
6. $myVar1->toString();
7. $yourVar1 = new erYourClass1();
8. $yourVar1->toString();
9. ?>
The above code will result output:
Class 'erMyClass2'
Class 'erYourClass1'