PHP/Class/ autoload — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 10:00, 26 мая 2010
Содержание
__autoload() function automatically includes a class file.
<source lang="html4strict">
function __autoload($classname) {
require_once("/includes/classes/$classname.inc.php");
}
</source>
__autoload( ) is called whenever you try to create an object of a class that hasn"t been defined
<source lang="html4strict">
<?
function __autoload($Class) { print "Bar class name: $Class!\n"; include "barclass.php"; } $foo = new Bar; $foo->wombat( );
?>
</source>
Automatically Loading Include Files with ___autoload()
<source lang="html4strict">
<?php function ___autoload ($class) {
$file = "$class.inc.php"; include_once( $file );
} $test = new YourClassName(); ?>
</source>
Creating an __autoload() Function
<source lang="html4strict">
<?php function __autoload($classname) {
require_once "class-{$classname}.php";
} $mydisk = new cd(); ?>
</source>
Using the __autoload() Function
<source lang="html4strict">
<?php
function __autoload($class) { $files = array("MyClass" => "/path/to/myClass.class.php", "anotherClass" => "/path/to/anotherClass.class.php"); if(!isset($files[$class])) return; require_once($files[$class]); } $a = new MyClass; $b = new anotherClass;
?>
</source>