I put in an example in __autoload, but it is useful here, too...
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
define('IGNORE_CASE',true);
function __autoload($class_name) {
static $possible_path = NULL;
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"
);
if (NULL===$possible_path):
$possible_path = array_flip(array(
"."
,".."
,"../include"
,"/public_html/php/include"
));
$possible_path = array_keys(array_merge($possible_path,
array_flip(explode(ini_get("include_path"),";"))));
endif; $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 ) {
if ( !$file->isDir()
and ( defined(IGNORE_CASE) && TRUE===IGNORE_CASE )
? stripos($file->getFileName(),$class_name)
: strpos($file->getFileName(),$class_name) ) :
foreach ( $possibility as $compare ):
if ((defined(IGNORE_CASE) && TRUE===IGNORE_CASE )
? !strcasecmp($compare,$file->getFileName())
: $compare===$file->getFileName()
) {
include_once($compare);
return TRUE;
}
endforeach; endif;
} }
}
?>