Home    Files

software times™  Files...

March 31, 2009

Autoloading Objects in php5


Autoloading objects is a great idea but since the function only accepts one parameter, the object's name, it is difficult to specify the location of the classes in the file system. You could use a global variable which is a bad idea. My solution is to add the autoload function to the top of every class file. After you include any one class the others in the same directory can make use of the autoload function.

An added benefit is that, since the directory is always the current directory, you could have varios folders with classes anywhere in the file system. Once you have loaded any one file from a directory, you can autoload the other files in the same directory. Here is my code:

php
if (!function_exists('__autoload')) { function __autoload($className) { require_once $className . '.php'; } }
Denny Schlesinger


Autoloading Objects


Home    Files
Top
Copyright © Software Times, 2000, 2001, 2003. All rights reserved
Last updated March 8, 2009.