Blog |

drapeko

@author drapeko

PHP autoloading files

with one comment

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 ↓

Generators

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 )

auto_generator.php

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
E:projectsscriptsautoload_generator>php auto_generator.php Project true autoload_generated.php
parameters number: 4

target path: Project
recursive: true
output: file
result file: autoload_generated.php
TODO list
  1. 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.

top ↑

auto_ext_generator.php

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.

top ↑

Description of examples

Archive consists of

  1. Three questions that are examined in the article:
    1. question1.php
    2. question2.php
    3. question3.php
  2. autoload_generated.php – file generated by autoload generator script and is used by autoload_example.php
  3. autoload_example.php – example of autoload realization
  4. Project directory – data for tests
      Project
      ..Classes
      ....Children
      ......A.php                    class A implements I1 {}
      ....B.php                      class B implements I2 {}
      ....Class_C.php                class C extends B implements I1, I3 {}
      ..Interfaces
      ....I1.php                     interface I1 {}
      ....blablabla.php              interface I2 extends I1 {}
      ....I3.php                     interface I3 {}

These examples are considered in “Autoloading in PHP” article.  read it→

top ↑

  • Share/Save/Bookmark

Written by rdrapeko

March 29th, 2009 at 1:00 pm

Posted in Default

One Response to 'PHP autoloading files'

Subscribe to comments with RSS or TrackBack to 'PHP autoloading files'.

  1. rdrapeko

    4 Apr 09 at 3:11 pm

Leave a Reply

Promotions (coming soon)