 |
很多开发者写面向对象的应用程序时对每个类的定义建立一个
PHP 源文件。一个很大的烦恼是不得不在每个脚本(每个类一个文件)开头写一个长长的包含文件列表。
在 PHP 5 中,不再需要这样了。可以定义一个
__autoload 函数,它会在试图使用尚未被定义的类时自动调用。通过调用此函数,脚本引擎在
PHP 出错失败前有了最后一个机会加载所需的类。
注意:
在 __autoload 函数中抛出的异常不能被
catch
语句块捕获并导致致命错误。
例 19-7. Autoload 例子
本例尝试分别从 MyClass1.php
和 MyClass2.php 文件中加载
MyClass1 和
MyClass2 类。
<?php function __autoload($class_name) { require_once $class_name . '.php'; }
$obj = new MyClass1(); $obj2 = new MyClass2(); ?>
|
|
emcmanus at gmail dot com
17-Jul-2007 01:04
Note: if you're experiencing unexpected "failed opening required 'filename.php' (include..." errors:
If you placed your autoload function in an external file which you're requiring at the head of every script, be cautious of some odd behavior regarding PHP's idea of the current working directory.
I ran into some unexpected path issues when my include file was placed in a subdirectory directory. The solution to my problems was to make sure that the autoload script being included is in the same directory as the calling script.
torch at torchs dot net
10-Jul-2007 05:33
I have just noticed that __autoload function is also called when extending classes.
For example:
<?php
// This is file called class.test1.php
class test1
{
/* class body */
}
?>
<?php
// This is file class.test2.php
class test2 extends test1
{
/* class body */
}
?>
<?php
// This is the file that contains __autoload
function __autoload($class)
{
require_once($class . ".php");
}
?>
The file "class.test1.php" will be reqire'd even though it is not implicitly called by new keyword.
alexandre at nospam dot gaigalas dot net
08-Jul-2007 05:33
Here is a simple version of __autoload for creating custom classes on the fly, extending pre-defined (using a constant) base classes:
<?php
define('CUSTOM_CLASS_TYPES', 'Application|Database|XML');
function __autoload($req) {
if (preg_match('#(.*)('.CUSTOM_CLASS_TYPES.'){1}$#', $req , $fragment)) eval('class '.$req.' extends base'.$fragment[2].' { public $_name = \''.$fragment[1].'\'; public $_type = \''.$fragment[2].'\'; } ');
}
/* Base Classes */
class baseApplication {}
class baseDatabase {}
class baseXML{}
/* Calling a custom Class */
$foo = new projectApplication;
print_r($foo);
?>
Krzysztof 'ChanibaL' Bociurko
06-Jul-2007 05:28
It should be noted that after you throw the exception using the fake evaled class, __autoload() will NOT run anymore with this class, because of the already existing fake one, so the second time you try new UndefinedClass() there will be NO exception, however if you throw an Exception in the __construct() of the fake class it will be cought.
Example code:
<?php
function __autoload($class) {
if(file_exists($f="{$class}.php"))
require_once($f);
else {
$errorText="Could not find class {$class}, searched in {$f}";
eval("class {$class} { "
."public function __construct() {throw new ClassNotFoundException('$errorText');}"
.'};');
throw new ClassNotFoundException($errorText);
}
}
?>
Unfortunately this still doesn't work 100% correct, because it relies on class initialization, therefore static sets/gets/calls will produce a fatal error if the unexisting class was being tried to be accessed before, as in:
<?php
try {
new Undefined();
}
catch (ClassNotFoundException $e) {
// the problematic fake class Undefined should exist by now...
}
Undefined::$staticVar='some value';
print Undefined::$staticVar;
print Undefined::staticMethod();
print Undefined::SOME_CONST;
?>
Overloading the __set(), __get() or __call() won't help, they don't work with statics.
dave
28-Jun-2007 04:46
a static classlist can speed up the autoload in a arbitrary folder structure.
each file holds one class with filename = classname + .php
<?php
define ("CLASS_PATH", "path to your classes");
function __autoload($className) {
static $classes = null;
if (!$classes) $classes = loadClasses(CLASS_PATH);
require_once($classes[$className]);
}
function loadClasses($path) {
$classes = array();
$h = @opendir($path);
if ($h) while ($file = @readdir($h)) {
if ($file=='.' || $file=='..') continue;
if(is_dir($path.$file)) $classes = array_merge($classes, loadClasses($path.$file."/"));
else $classes[array_shift(explode(".", $file))] = $path.$file;
}
return $classes;
}
?>
peter dot gooman at gmail dot com
18-Jun-2007 03:33
Before you start using __autload, remember that it holds no scope/namespace. This means that if you are depending on third party applications and they have an autoload function defined and so do you, your application will error.
To remedy this, everyone should look at the spl_autoload functions, eg: spl_autoload_register. This function allows more than one custom functions to be called through the default spl_autoload (default __autoload) handler.
Andrea Giammarchi
21-Mar-2007 11:54
Another workaround for Exception problem (Klaus Schneider style)
<?php
define('CLASS_DIR', 'php/classes/');
function __autoload($name) {
if($exists = !class_exists($name) && file_exists($class = CLASS_DIR.$name.'.class.php'))
require $class;
elseif(!$exists) {
eval('class '.$name.' extends Exception {}');
throw new $name('[__autoload] this file doesn\'t exists: '.$class);
}
}
try {
new Undefined;
}
catch(Undefined $e) {
echo $e->getMessage();
}
// You should use generic Exception too
catch(Exception $e) {
echo $e->getMessage();
}
?>
Klaus Schneider
01-Mar-2007 03:35
I just stumbled over one quite nice solution to the __autoload-exception problem. It allows for any kind of exception to be thrown inside __autoload().
It appears one has to define the requested class (using "eval", which is not nice but inevitable here) and after that can simply throw an exception (and catch it if so desired):
<?php
function __autoload($className)
{
// Do your stuff to load a class here, set $ok if everything went fine.
if (! $ok) {
eval("class $className{};");
throw new Exception('My message');
} // if
}
try {
UndefinedClass::undefinedFunction();
} catch (Exception $ex) {
echo $ex->getMessage();
} // try/catch
?>
Output: "My Message".
:-)
p dot scheit at zweipol dot net
14-Feb-2007 12:49
If you really wan't to use something like Exceptions, but you don't want to use eval (like me) I have a suggestion for you.
This is "oldschool" Try and Catching by checking the return value.
$cfgLine = $this->generateMapping($className,NULL,TRUE);
if ($cfgLine instanceof Exception) {
$e = $cfgLine;
trigger_error('uncaught exception: '.$e->getMessage().' '.$e->getTraceAsString(), E_USER_ERROR);
}
you get the point...
This looks like a usual uncaught Exception.
It's a little bit tricky to check all return values from your Methods, but you can catch and throw something ... better than doing nothing
david dot thalmann at gmail dot com
02-Feb-2007 03:19
Note to Ricos posting:
A lot of useless Coding. However, I improved the code, so now it will be able to find any folders ("." and ".." will not being tested... oO) and search as deep as possible. Now it will find CLASS_DIR/foo/bar.class.php also like CLASS_DIR/foo/bar/baz/buz/fii/and/so/on/class.php
Warning: This code will check ALL dirs who're "deeper" / "lower" than the class dir, so prevent deeply hidden files (or use just a few folders).
Improved Version:
<?php
// change this, if this code isn't "higher" than ALL classfiles
define("CLASS_DIR", dirname(__FILE__));
/**
* autoload classes (no need to include them one by one)
*
* @uses classFolder()
* @param $className string
*/
function __autoload($className) {
$folder = classFolder($className);
if($folder)
require_once($folder.$className.".class.php");
}
/**
* search for folders and subfolders with classes
*
* @param $className string
* @param $sub string[optional]
* @return string
*/
function classFolder($className, $sub = "/") {
$dir = dir(CLASS_DIR.$sub);
if(file_exists(CLASS_DIR.$sub.$className.".class.php"))
return CLASS_DIR.$sub;
while(false !== ($folder = $dir->read())) {
if($folder != "." && $folder != "..") {
if(is_dir(CLASS_DIR.$sub.$folder)) {
$subFolder = classFolder($className, $sub.$folder."/");
if($subFolder)
return $subFolder;
}
}
}
$dir->close();
return false;
}
?>
Rico
04-Jan-2007 01:00
This autoload function searches for the class Location before requiring it. So there's no need of putting the classes all in one folder.
Requirements:
- the subfolders must be at least 3 letters long
- the filenames must be in the form CLASSNAME.class.php
Note:
- in this example the main class folder is 'lib'
define('ROOT_DIR', dirname(__FILE__).'/');
function __autoload($className) {
$folder=classFolder($className);
if($folder) require_once($folder.'/'.$className.'.class.php');
}
function classFolder($className,$folder='lib') {
$dir=dir(ROOT_DIR.$folder);
if($folder=='lib' && file_exists(ROOT_DIR.$folder.'/'.$className.'.class.php')) return $folder;
else {
while (false!==($entry=$dir->read())) {
$checkFolder=$folder.'/'.$entry;
if(strlen($entry)>2) {
if(is_dir(ROOT_DIR.$checkFolder)) {
if(file_exists(ROOT_DIR.$checkFolder.'/'.$className.'.class.php')) return $checkFolder;
else {
$subFolder=classFolder($className,$checkFolder);
if($subFolder) return $subFolder;
}
}
}
}
}
$dir->close();
return 0;
}
sandrejev at gmail dot com
09-Nov-2006 07:23
Here is the most complete version of __autoload exception i guess.
The best thing is that it can throw any exception plus the exception is fully functional.
<?php
class AutoloadException extends Exception { }
class AutoloadExceptionRetranslator extends Exception
{
public function __construct($serializedException)
{
throw unserialize($serializedException);
}
}
function __autoload($classname)
{
if(!file_exists($classname))
{
$autoloadException = serialize(new AutoloadException("Class $classname could not be found"));
return eval("
class $classname
{
function __construct(\$a=0, \$b=0, \$c=0, \$d=0, \$e=0, \$f=0, \$g=0, \$h=0, \$i=0)
{
throw new AutoloadExceptionRetranslator('$autoloadException');
}
}
");
}
else
{
require_once $classname;
}
}
try
{
$anyObject = new AnyNonExistantClass();
}
catch (AutoloadException $e)
{
print_r($e->getTrace());
}
?>
andrew dot delete dot cornes at gmail dot delete dot com
03-Nov-2006 12:26
If you'd like '__autoload()' to support multiple class folders, each containing multiple class files (one per class), you may want to try something like this (file '__autoload.php'):
<?php
define('CLASS_FILENAME_SUFFIX', '.class.php');
function __autoload($className)
{
$__autoloadAbsolutePath = dirname(__FILE__);
// 'pathStart' is your web application root folder.
// (This may or may not be where '__autoload.php'
// resides; let's assume here that it resides one
// level 'below' the web app root.)
$pathStart = $__autoloadAbsolutePath .
DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR;
// 'classPath' is a list of class folders to look in.
// (In this example, there's just one: 'classlibs/lib1'.
// To add more, simply append them; start with
// 'PATH_SEPARATOR . $pathStart .', and off you go...)
$classPath = PATH_SEPARATOR . $pathStart .
'classlibs' . DIRECTORY_SEPARATOR . 'lib1';
// Add list of class folders to 'include_path' for the
// forthcoming 'require()' (or similar directive).
$oldIncludePath = get_include_path();
set_include_path($oldIncludePath . $classPath);
require_once($className . CLASS_FILENAME_SUFFIX);
// Reinstate initial 'include_path'.
set_include_path($oldIncludePath);
}
?>
As your web application develops, new paths containing class files can be added into the '$classPath' variable within '__autoload()'. If hard-coding the '$classPath' variable isn't to your taste, you could arrange for its value to come from 'outside' in whatever way you like.
Any comments gratefully received.
Chris Corbyn (chris AT w3style.co.uk)
08-Sep-2006 01:23
I'm sure this is needed by more than me.
My objective was to allow __autoload() to be easily extended in complex systems/frameworks where specific libraries etc may need loading differently but you don't want to hard-code little adjustments into your working __autoload() to allow this to happen.
Using a ServiceLocator object with some static methods and properties to allow loosely coupled locators to be attached to it you can swap/change and add to the functionality of your __autoload() at runtime.
The core stuff:
<?php
/**
* Defines the methods any actual locators must implement
* @package ServiceLocator
* @author Chris Corbyn
*/
interface Locator
{
/**
* Inform of whether or not the given class can be found
* @param string class
* @return bool
*/
public function canLocate($class);
/**
* Get the path to the class
* @param string class
* @return string
*/
public function getPath($class);
}
/**
* The main service locator.
* Uses loosely coupled locators in order to operate
* @package ServiceLocator
* @author Chris Corbyn
*/
class ServiceLocator
{
/**
* Contains any attached service locators
* @var array Locator
*/
protected static $locators = array();
/**
* Attach a new type of locator
* @param object Locator
* @param string key
*/
public static function attachLocator(Locator $locator, $key)
{
self::$locators[$key] = $locator;
}
/**
* Remove a locator that's been added
* @param string key
* @return bool
*/
public static function dropLocator($key)
{
if (self::isActiveLocator($key))
{
unset(self::$locators[$key]);
return true;
}
else return false;
}
/**
* Check if a locator is currently loaded
* @param string key
* @return bool
*/
public static function isActiveLocator($key)
{
return array_key_exists($key, self::$locators);
}
/**
* Load in the required service by asking all service locators
* @param string class
*/
public function load($class)
{
foreach (self::$locators as $key => $obj)
{
if ($obj->canLocate($class))
{
require_once $obj->getPath($class);
if (class_exists($class)) return;
}
}
}
}
/**
* PHPs default __autload
* Grabs an instance of ServiceLocator then runs it
* @package ServiceLocator
* @author Chris Corbyn
* @param string class
*/
function __autoload($class)
{
$locator = new ServiceLocator();
$locator->load($class);
}
?>
An example Use Case:
<?php
require 'ServiceLocator.php';
//Define some sort of service locator to attach...
class PearLocator implements Locator
{
protected $base = '.';
public function __construct($directory='.')
{
$this->base = (string) $directory;
}
public function canLocate($class)
{
$path = $this->getPath($class);
if (file_exists($path)) return true;
else return false;
}
public function getPath($class)
{
return $this->base . '/' . str_replace('_', '/', $class) . '.php';
}
}
// ... attach it ...
ServiceLocator::attachLocator(new PearLocator(), 'PEAR');
// ... and code away....
$foo = new Foo_Test();
?>
gonix
03-Aug-2006 01:39
in response to alexey at renatasystems dot org:
You may add ``global $somedata;`` before ``$somedata = 'Some data';`` and it should work as expected.
file bar.class.php:
<?php
global $somedata;
$somedata = 'Some data'; /* global scope in common way */
class bar {
function __construct()
{
global $somedata; /* reference to global scope variable */
if ( isset($somedata) )
{
var_dump($somedata);
}
else
{
die('No data!');
}
}
}
?>
'common way':
<?php
require 'bar.class.php';
$foo = new bar();
?>
'__autoload way':
<?php
function __autoload($classname)
{
require $classname . '.class.php';
}
$foo = new bar();
?>
Both 'comon way' and '__autoload way' should give same result:
string(9) "Some data"
alexey at renatasystems dot org
06-Jul-2006 07:15
While using an "autoloading" method you should pay attention to variables scope. Because of new file will be included INSIDE of magic function __autoload - all of declared in such file global scope variables will be only available within this function and nowhere else. This will cause strange behaviour in some cases. For example:
file bar.class.php:
<?php
$somedata = 'Some data'; /* global scope in common way */
class bar {
function __construct()
{
global $somedata; /* reference to global scope variable */
if ( isset($somedata) )
{
var_dump($somedata);
}
else
{
die('No data!');
}
}
}
?>
Attempt to load this file in common way:
<?php
require 'bar.class.php';
$foo = new bar();
?>
this will output (as expected):
string(9) "Some data"
But in case of __autoload:
<?php
function __autoload($classname)
{
require $classname . '.class.php';
}
$foo = new bar();
?>
you could expect that this script will return the same but no, it will return "No data!", because defenition of $somedata after requiring treats as local within user-defined function __autoload().
phpman at generic dot depoll dot de
16-May-2006 09:47
"Inspired" by unspammable-iain at iaindooley dot com, here is another approach to the dreaded PHP_INCOMPLETE_CLASS problem:
<?php
/**
* Provides an include function which caches the path to a class in order to provide a better autoload functionality.
*
* While the Includer::doInclude() function takes a a string as an argument, which includes path information, __autoload() will
* always get a "naked" classname. In order to find the according path for that class, we use our cache which essentially is a hash
* of the form array(className => filePath). The inevitable constraint of that approach is obvious: we can't remember several
* pathes for one and the same classname, meaning that it either has to be unique or you have to use several Includers for your
* different, but equally named, classes.
*
* @author "Niels Ganser" <phpman@generic.depoll.de>
*/
class Includer {
static private $instance;
private $includePaths = array();
private $hasChanged = false;
/**
* We use Singeltons here, so please use this function to get an Includer instance.
*
* @return Includer An instance of Includer. And only one.
*/
public function instance() {
if (empty(self::$instance))
self::$instance = new Includer(INCLUDE_PATH_CACHE);
return self::$instance;
}
/**
* @param string Path to the cache file. Obviously has to be read- and writable by the webserver user.
*/
private function __construct($cacheFile) {
if (file_exists($cacheFile))
$this->includePaths = unserialize(file_get_contents($cacheFile));
}
/**
* Checks if the path to a class with the given name is cached.
*
* @param string $className Name of a class
* @return boolean
*/
static public function isCached($className) {
return (isset(self::instance()->includePaths[$className])) ? true : false;
}
/**
* Returns the cached path for the given classname or null if there is nothing cached.
*
* @param string $className Name of a class
* @return mixed A filepath (string) which we remember as leading to the given class or null if we have
* no clue as to where to look for the class.
*/
static public function getPath($className) {
return (self::isCached($className)) ? self::instance()->includePaths[$className] : null;
}
/**
* Includes (or requires) a given class.
*
* @param string $class The path to our class in a package.className format.
* I.e. "classes.money.AUD" would lead to "classes/money/AUD.php".
* @param boolean $required If true, we require the file instead of just trying to include it.
* @return boolean Return value of the require_once resp. include_once statement.
*/
static public function doInclude($class, $required = false) {
$className = substr($class, strrpos($class, '.')+1);
$path = str_replace('.', '/', $class) .'.php';
if (!isset(self::instance()->includePaths[$className]) or self::instance()->includePaths[$className] != $path) {
self::instance()->includePaths[$className] = $path;
self::instance()->hasChanged = true;
}
return ($required) ? require_once $path : include_once $path;
}
/**
* Writes the cache to disk, if its contents have changed.
*/
public function __destruct() {
if (self::instance()->hasChanged)
file_put_contents(INCLUDE_PATH_CACHE, serialize(self::instance()->includePaths));
}
}
?>
It has the smell of a workaround but I think it is a rather clean one. And here is a simple __autoload function for use with Includer:
<?php
function __autoload($className) {
if (Includer::isCached($className))
include_once Includer::getPath($className);
else
trigger_error('Cannot find '. $className, E_USER_ERROR);
}
?>
Of course you can extend that function to use other loading mechanisms if Includer::isCached() returns false. Also please note, that in the above implementation INCLUDE_PATH_CACHE has to the full path to a web server writable file.
The inline documentation is shortened a bit. The complete file is on http://ng.nerdking.de/static/php/Includer.phps
Cheers,
Niels
RQuadling at GMail dot com
08-Mar-2006 08:55
An issue I've had with using the __autoload function is getting it into the application.
You have to have the function included in every topmost script. This is a pain if the entire application is OOP and an "app" can be just a component of another "app".
A solution I've found is to use php.ini's auto_prepend_file setting.
Mine is set to ...
auto_prepend_file = auto_loader.php
The auto_loader.php script contains a single function. The __autoload() function.
The include_dir path IS examined to find this file, so you can just put it with the rest of your includable files.
A useful additional facility here is that you could log which classes are used by a script at runtime. Very useful if you have object factories and can't know the load at design time.
Also, assigning the uncaught exception handler and the error handlers in this file means your entire site WILL have some global protection without you having to deal with it on a script by script basis.
If you do not have access to the PHP.INI file, or you are running on a shared server, you may not be able to set this property. In those cases, you may be able to set the value using .htaccess. (NOTE: UNTESTED as I don't use Apache).
<IfModule mod_php5.c>
php_value auto_prepend_file "auto_loader.php"
</IfModule>
You COULD therefore have a different set of rules per subdomain (if you have multiple subdomains, say, live, test, beta, devel) or whatever takes your fancy.
For more details on this see the "Description of core php.ini directives" (http://www.php.net/manual/en/ini.core.php)
steven at gandso dot com
07-Mar-2006 08:10
An alternative to __autoload for PHP4.
<?php
/**
* Set the variable
**/
$string = "";
/**
* Find pathnames matching the pattern
**/
foreach( glob( $_SERVER['DOCUMENT_ROOT'] . "/classes/class.*.inc" ) as $filename )
{
/**
* Include and evaluates the specific file
**/
require_once( $filename );
/**
* Get the classnames
**/
if( preg_match( "/class\\.(.+?)\\.inc/", $filename, $className ) )
{
/**
* Create a variable of the desired type with the 'new' operator
**/
$string .= "$" . $className[1] . " = new " . ucfirst( $className[1] ) . ";";
}
}
/**
* Evaluate the string as PHP code
**/
eval( $string );
?>
unspammable-iain at iaindooley dot com
07-Feb-2006 02:44
in response to the post by UNSPAMMABLEkai at kaiundina dot de, i have another interesting implementation of the __autoload() system.
i have all my classes in their own file, where ThisClassName would be in the file this_class_name.class.php (Ruby inspired :-)
i really like having all class dependencies at the top of the class file, because you can tell at a glance what packages/classes a particular class uses/depends on, but this meant big loading times for huge class heirarchies, even if all those classes were not being used.
so i created my own require function that uses a serialised array of include paths, and then an __autoload() function that will get the required included path when it is needed _only_. this also allows for nice and liberal directory structures.
Here is the file with all the implementation in it:
<?
@$contents = file_get_contents('managed_code/includes.php');
if($contents)
$include_paths = unserialize($contents);
else
$include_paths = array();
function __autoload($class)
{
if($path = rsIncludePath($class))
require_once($path);
else
{
echo('You need to call rsRequire() before using a class: '.$class);
debug_print_backtrace();
}
}
function rsIncludePath($class)
{
global $include_paths;
$ret = '';
if(isset($include_paths[$class]))
$ret = $include_paths[$class];
return $ret;
}
function rsRequire($class,$required = true)
{
global $include_paths;
$classname = rsClassName($class);
$path = preg_replace('/\./','/',rsClasstoFile($class));
$path .= '.class.php';
if((!file_exists('packages/'.$path)))
{
echo('couldn\'t load package: '.$class.'<br /><br />
');
if($required)
debug_print_backtrace();
}
else
{
$path = 'packages/'.$path;
if(isset($include_paths[$classname]))
{
if($include_paths[$classname]!=$path)
{
$include_paths[$classname] = $path;
rsPersist();
}
}
else
{
$include_paths[$classname] = $path;
rsPersist();
}
}
}
function rsClassName($class)
{
$split = preg_split('/\./',$class);
$class_name = $split[count($split)-1];
return $class_name;
}
function rsPersist()
{
global $include_paths;
$data = serialize($include_paths);
file_put_contents('managed_code/includes.php',$data);
}
?>
Now here is a class file:
<?
rsRequire('package.name.SomeBaseClass');
rsRequire('package.name.MightNotBeUsed');
class ThisClass extends SomeBaseClass
{
/**ALL MY CODE***/
}
?>
the class file for MightNotBeUsed would only be included if it was actually used in the class code. Also, notice that the above method is quite efficient because it only unserialises the array of include paths once, and will only reserialise the array in the case that it is a new include path, or the include path has changed. Also notice that this is 'session safe', meaning if you register a session object and then your application dies, you will not get class non-existing errors the next time you run it because the include paths are serialised as they are added. Enjoy!
dave60 /at/ gmail /dot/ com
29-Dec-2005 09:25
In reply to quetzalcoatl:
Generally, I would advise for each class to have it's own file, and hold nothing besides that class. Just define __autoload() in a/the infrastructure file -- a/the file that does the behavioral logic, and there should be no need to redefine it in a class' file.
me at mydomain dot com
12-Nov-2005 12:07
You can enable this behaviour for undefined classes while unserializing objects by setting the .ini-variable 'unserialize_callback_func' to '__autoload'.
quetzalcoatl(AT)poczta.fm
05-Nov-2005 11:21
While __autoloading is a nice feature, one can stumble upon small problem with it. Imagine:
file A.php:
<?
function __autoload($cname)
{ require_once "include/$cname.php";}
}
B::testC();
?>
file B.php:
<?
function __autoload($cname)
{ require_once "include/$cname.php";}
}
class B
{ public static function test()
{ C::testMe();
}
}
?>
file C.php:
<?
class C
{ public static function testMe()
{ print("OK!");
}
}
?>
Now, running file A.php will result in an error.. because from A's point of view, function __autoload is declared twice - once in A.php, and in required_once'd B.php, too! [Not mentioning that C.php and many many many other required or included files could use it too :) ] So, of course we can remove the duplicate __autoload functions from B.php and any other required files. Thus, we either disallow usage of B.php independently (and other required-able/include-able files!) , as without the __autoload it can not work -- or we have to manually require_once its dependecies, thus making manually the job of __autoload. Irritating, isn't it?
A simple trick can help us here. Every file you would place an __autoload function, place instead something like:
if(!function_exists("__autoload"))
{ //your __autoload declaration, for example:
function __autoload($cname)
{ require_once "include/$cname.php";
}
}
When I first tried it, I was really surprised that you can control functions' declaration with simple IF, without any evals or other tricks.
Well.. it's probably a basic "trick", but I didn't notice it anywhere. Feel free to remove it if it was already posted somewhere:)
info at tobiasdemuth dot de
03-Nov-2005 09:02
Hey there,
I've published c^2, an autoloading-system with self-modificating cache and directory crawler under www.tobias-demuth.de.
License is LGPL - just include it and don't think about class-inclusion anymore!
kind regards
Tobias
shurikk at gmail dot com
06-Oct-2005 09:33
one more thing about my previous example/comment (there is a bug :) )
--- file a.php should be like this
<?php
class A {
public $a;
function __construct($a) {
$this->a = $a;
}
// our kinda magic method
public function getClassFile() {
return __FILE__;
}
}
?>
--- end a.php
one more file to test the example
--- b1.php
<?php
include('Session.php');
$a = new A();
$a = Session::put('a', $a);
// go to b.php now
?>
--- end b1.php
open b1.php, and then b.php in your browser.
in the Session class you might want to implement remove() method.
thanks
--alex
shurikk at gmail dot com
06-Oct-2005 08:31
somebody might find this "quick and dirty" example useful (see the code below). BTW, is there a magic metod that tells you what the class file name is? If none, perhaps a feature request?
What this example about: store objects in the session and restore them without worry of what external files to be "required" (solves the problem when you get incomplete PHP object, when your class is not declared prior unserialize())
PHP5, but easy to adapt for PHP4
--- file Session.php
<?php
session_start();
// don't do any comments, nothing special in this class IMO
class Session {
public static function has($name) {
return isset($_SESSION[$name]);
}
public static function get($name) {
if(Session::has($name)) {
if(Session::has('__requireFiles')) {
$requireFiles = unserialize($_SESSION['__requireFiles']);
if(array_key_exists($name, $requireFiles)) {
require_once($requireFiles[$name]);
}
}
return unserialize($_SESSION[$name]);
}
return null;
}
public static function put($name, $value) {
if(is_object($value)) {
if(method_exists($value, 'getClassFile')) {
if(!($requireFiles = Session::get('__requireFiles')))
$requireFiles = array();
$requireFiles[$name] = $value->getClassFile();
Session::put('__requireFiles', $requireFiles);
}
}
$_SESSION[$name] = serialize($value);
}
}
?>
--- end Session.php
--- file a.php
<?php
include('Session.php');
// an object, all as usuall, but with one magic method
// getClassFile(), you can call it whatever you like
class A {
public $a;
function __construct($a) {
$this->a = $a;
}
// our kinda magic method
public function getClassFile() {
return __FILE__;
}
}
$a = new A('test');
Session::put('a', $a);
}
?>
--- end a.php
--- file b.php
<?php
include('Session.php');
// everybody's happy :)
$a = Session::get('a');
?>
--- end b.php
kai at kaiundina dot de
20-Sep-2005 07:34
To: gomasj at gmail dot com
I strongly disagree in that "no well organized programmer will need to use it".
The method reduces the overhead when loading unneeded classes while encouraging the use of well organized class libraries.
Assume you have a class needing an undetermined number of other classes to operate properly - let's say an XML-parser that parses a document into an object tree (one class per element is a typical procedure).
A common behavior was to load all possibly needed classes in advance - regardless if they would be used ever. That would be a performance deasaster for the above case.
Another possibility was to load the particular class when needed - providing a path to the file containing the class declaration. Since relative path's in PHP are calculated relative to the script handling the client request, they're some kind of complex to manage in huge projects, where the script can be called from various locations.
If you're willing to move a class's declaration file to another location (directory) you have to update each reference to that file.
Lot's of well organized programmers (so are you), wrote their own mechanisms so solve these problems. But you always had to make at least one call to load the class file each time it might possibly be referenced for the first time in code execution - that are:
- new
- extends
- implements
- Type hints
- static reference (class::xxx)
- deserialization of sessions
__autoload() simply provides an event handler to centralize the loading of a class's declaration only when it's needed for the first time. Regardless where or by what the request occurred.
So it keeps your code clean, fast and allows you to reorganize your class-library as you like without modifying any of your source files (except the autoload-function itself).
We use the autoload feature in a 200,000 lines php-project as follows:
The 'ClassManager' maintains a cache file containing the file paths to each class-declaration. Whenever an unknown class (not present in the cache, yet) is requested, it searches the directories for a file containing the desired class declaration and adds it to the cache for the next time. This method allows us to modify our class-library at will. We can add, move or remove classes without adjusting a single line of code. When our software starts up, the ClassManager has to handle about 6,000 class requests - without having to worry about performance reasons.
I am developer and project manager in the project mentioned above (a web based cms) - so I'd call myself well organized.
Anyway,
thanks for your opinion
php at kaiundina dot de
20-Sep-2005 06:42
The autoload-feature allows to add the behavior of static constructors (like in C#). Static constructors should be called on the first occurence of a class reference - typically a 'new' operator or a static call to a class's operation.
They can be used used to initialize complex static properties.
And here is an easy and save way how it can be done:
Content of MyClass.class.php5:
<?php
// demo class persisting of a static and a dynamic constructor
class MyClass
{
// static constructor operation
public static function _construct()
{
// just say hello
echo '<div>static constructor</div>';
}
// default dynamic constructor operation
public function __construct()
{
// just say hello
echo '<div>dynamic constructor</div>';
}
}
?>
Content of index.php5:
<?php
// declare handler for any unknown class request
function __autoload($aClassName)
{
// load the class
require_once ($aClassName . '.class.php5');
// create a reference to the static constructor's operation
$staticConstructorReference = array($aClassName, '_construct');
// if the static constructor was declared properly...
if (is_callable($staticConstructorReference))
{
// call the static constructor
call_user_func($staticConstructorReference);
}
}
// create an example object to see both constructors being executed
$article = new MyObject();
?>
andy bravery
16-Sep-2005 09:37
kencomer, I think there is a problem with your function below in that if strpos() or stripos() locate your class name at the beginning of the filename string then they return a position 0 which gets interpretted as a boolean FALSE. For me this meant my file was never found.
kencomer at NOSPAM dot kencomer dot com
14-Sep-2005 01:46
Yet another class/interface __autoload function. Includes an example usage of the SPL DirectoryIterator class, a settable case-ignore flag, and support for multiple file name patterns to allow easy integration from multiple sources.
<?php
/**
* __autoload
*
* @author Ken Comer
* @copyright released into public domain 2005 Ken Comer
*/
define('IGNORE_CASE',true);
// comment out the define() of IGNORE_CASE to be
// case-sensitive. I like to ignore case so that I can
// use UPPERCASE for the test versions of the file.
/**
* autoloads classes and interfaces for PHP5
*
* @author Ken Comer
*/
function __autoload($class_name) {
// This will be set the first time through.
// Put your default values in the place indicated
// below so as to eliminate possible duplicates
// in the .ini include_path
static $possible_path = NULL;
// Leave this as NULL.
// List here whatever formats you use for your
// file names. Note that, if you autoload
// a class that implements a non-loaded interface,
// you will also need to autoload that interface.
static $permitted_formats = array(
"&CLASS.class.inc"
,"&CLASS.class.inc.php"
,"&CLASS.class.inc.php5"
,"class.&CLASS.inc"
,"class.&CLASS.inc.php"
,"class.&CLASS.inc.php5"
,"&CLASS.interface.inc"
,"&CLASS.interface.inc.php"
,"&CLASS.interface.inc.php5"
,"i&CLASS.interface.inc"
,"i&CLASS.interface.inc.php"
,"i&CLASS.interface.inc.php5"
);
// Put the &CLASS wherever the $class_name
// might appear
// Only executed the first time __autoload is called
if (NULL===$possible_path):
// These are the default paths for this application
$possible_path = array_flip(array(
"."
,".."
,"../include"
,"/public_html/php/include"
));
// Customize this yourself, but leave the
// array_flip alone. We will use this
// to get rid of duplicate entries from the
// include_path .ini list.
// Merge the flipped arrays to get rid of duplicate
// "keys" (which are really the valid include
// paths) then strip out the keys leaving only
// uniques. This is marginally faster than
// using array_combine and array_unique and
// much more elegant. Okay, it's weird, too.
$possible_path = array_keys(array_merge($possible_path,
array_flip(explode(ini_get("include_path"),";"))));
endif; /* static $possible_path initialization */
$possibility = str_replace("&CLASS",$class_name,$permitted_formats);
foreach ( $possible_path as $directory ) {
if (!file_exists($directory) or !is_dir($directory))
{
continue;
}
$file_to_check = new DirectoryIterator($directory);
foreach ( $file_to_check as $file ) {
// ignore directories and files that do not contain
// $class_name
if ( !$file->isDir()
and ( defined(IGNORE_CASE) && TRUE===IGNORE_CASE )
? stripos($file->getFileName(),$class_name)
: strpos($file->getFileName(),$class_name) ) :
// class_name was included, now compare against
// all permitted file name patterns
foreach ( $possibility as $compare ):
if ((defined(IGNORE_CASE) && TRUE===IGNORE_CASE )
? !strcasecmp($compare,$file->getFileName())
: $compare===$file->getFileName()
) {
// by using $compare, you will get a qualified
// file name
include_once($compare);
return TRUE;
}
endforeach; /* $possibility */
endif;
} /* foreach $file_to_check */
}
}
?>
sr at brightlight dot ch
25-Jul-2005 09:52
If you want to autoload functions you can do this with a little work-around:
<?php
class utilities
{
protected static $instance;
protected function __construct()
{
}
public function get()
{
if (isset(self::$instance)) {
return self::$instance;
} else {
self::$instance = new utilities();
|