PHP autoloading files
THIS PAGE NOW WILL BE REDIRECTED TO A NEW VERSION OF THIS ARTICLE
If you are interested in these files, you probably will be interested in article “Autoloading in PHP”. read it→
Simple and extended generators: download (4.6 KB) 07 05 2009 →, description ↓
Examples for article: download (4.0 KB) →, description ↓
autoload_generator.rar, download (4.6 KB) →
Archive contains two files: auto_generator.php (version 1.2) (description ↓) and auto_ext_generator.php (version 1.1) (description ↓)
This script is specially designed to scan folders and identify locations of the classes by using regular expressions. The result produced by the script is an associative array of classes/interfaces, their locations and extended/implemented classes/interfaces. The sample result is showed below:
<?php
$autoload_list = array (
'classes' => array (
'A' => array ('path' => 'ProjectClassesChildrenA.php',
'extends' => array (), 'implements' => array ('I1')),
'B' => array ('path' => 'ProjectClassesB.php',
'extends' => array (), 'implements' => array ('I2')),
'C' => array ('path' => 'ProjectClassesC.php',
'extends' => array ('B'), 'implements' => array ('I1', 'I3')),
),
'interfaces' => array (
'I2' => array ('path' => 'ProjectInterfacesblablabla.php', 'extends' => array ('I1')),
'I1' => array ('path' => 'ProjectInterfacesI1.php', 'extends' => array ()),
'I3' => array ('path' => 'ProjectInterfacesI3.php', 'extends' => array ()),
),
);
?>
Requirements
What do you need to use it? You have to have installed PHP server and ability to use command line tool. :) Script was written using PHP 5.2 version but probably it would work with all 5th versions. If you check on lower that 5.2 versions, please give a feedback.
Incoming parameters
What are the incoming parameters?
| Position | Type | Default value | Description |
|---|---|---|---|
| 1 | string | hardcoded location, root directory | a source dir path (folder where to search for the interfaces and classes) |
| 2 | boolean | false | is the search recursive or not |
| 3 | boolean/string | true | Where the result will be stored. If false, outputs the result; if true, result is stored in the file, default destination is used. If string, result is stored in this file. |
| 4 | string | hardcoded name (“generated_array”) | a name of the generated array |
Default behavior
If you want to change the default behavior of the script, you could change values of the variables that are marked // CHANGABLE
// CHANGEABLE. Default location of the target folder (if you don't use first parameter). $target = dirname(__FILE__); // CHANGEABLE. Are results passed to the file? (if you don't use third parameter) $v_save_to_file = true; // CHANGEABLE. Default result file. (if you don't use third parameter) $v_save = $target.DIRECTORY_SEPARATOR."autoload_generated.php"; // CHANGEABLE. Is search recursive? Default value. (if you don't use 2nd parameter) $v_recursive = true; // CHANGEABLE. Name of the generated array. $v_array_name = 'autoload_list';
Example of usage
TODO list
- Found bug in 132-133 lines of 1.1 version. Some useful source code was erased while deleting the code. Bug was fixed in 1.2 version on 07 May 2009. FIXED
Comments
Please notice that the current version of the script does not transform relative paths into absolute. If you use a relative path as the target point parameter, you will get an array where locations of the classes/interfaces are also relative. To avoid errors please use absolute path as the first parameter.
This script extends the functionality of auto_generator.php. It adds new feature to scan in several folders. It’s not designed to have incoming parameters.
If you have new autoloading taks, you should copy auto_ext_generator.php and change values of variables as you want.
Requirements
See auto_generator.php
Variables
// CHANGEABLE. The list of directories to be processed.
$v_dirs = array (
// array('location', 'is_recursive')
array(
'path' => 'D:from_computerrom_projectsdialogue',
'recursive' => true
),
array(
'path' => 'D:from_computerdangausapp',
'recursive' => true
),
array(
'path' => 'D:from_computerrom_projectsdialogue',
'recursive' => true
),
);
// CHANGEABLE. The result file.
$v_save = 'E:projectsscriptsautoload_generatorautoload_generated.php';
// CHANGEABLE. The name of the generated array;
$v_array_name = 'autoload_list';
Comments
Please notice that this script depends on auto_generator.php. It won’t work without it.
Archive consists of
- Three questions that are examined in the article:
- question1.php
- question2.php
- question3.php
- autoload_generated.php – file generated by autoload generator script and is used by autoload_example.php
- autoload_example.php – example of autoload realization
- Project directory – data for tests
These examples are considered in “Autoloading in PHP” article. read it→
Read this article http://wp.drapeko.com/2009/03/28/autoloading-in-php/
[Reply]
rdrapeko
4 Apr 09 at 3:11 pm