note 91119 added to language.oop5.autoload
| From: | ranskillsatyahoodotcodotuk at osu1 dot php dot net | Date: | Tue, 26 May 2009 11:02:09 +0000 |
| Subject: | note 91119 added to language.oop5.autoload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156015@lists.php.net to get a copy of this message | ||
This custom autoload function allows for the flexibility to add directories from which the classes
are expected to be found and loads classes/interfaces with different file naming formats.
Additionally, it handles classes included in the include path and files in the PEAR
package/directory.
You can check this blog entry for a video tutorial http://ranskills.com/blog/.
<?php
/**
*
* @param string $className Class or Interface name automatically
* passed to this function by the PHP Interpreter
*/
function autoLoader($className){
//Directories added here must be
//relative to the script going to use this file.
//New entries can be added to this list
$directories = array(
'',
'classes/'
);
//Add your file naming formats here
$fileNameFormats = array(
'%s.php',
'%s.class.php',
'class.%s.php',
'%s.inc.php'
);
// this is to take care of the PEAR style of naming classes
$path = str_ireplace('_', '/', $className);
if(@include_once $path.'.php'){
return;
}
foreach($directories as $directory){
foreach($fileNameFormats as $fileNameFormat){
$path = $directory.sprintf($fileNameFormat, $className);
if(file_exists($path)){
include_once $path;
return;
}
}
}
}
spl_autoload_register('autoLoader');
?>
----
Server IP: 69.147.83.197
Probable Submitter: 41.204.40.15
----
Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php
Edit -- https://master.php.net/note/edit/91119
Del: integrated -- https://master.php.net/note/delete/91119/integrated
Del: useless -- https://master.php.net/note/delete/91119/useless
Del: bad code -- https://master.php.net/note/delete/91119/bad+code
Del: spam -- https://master.php.net/note/delete/91119/spam
Del: non-english -- https://master.php.net/note/delete/91119/non-english
Del: in docs -- https://master.php.net/note/delete/91119/in+docs
Del: other reasons-- https://master.php.net/note/delete/91119
Reject -- https://master.php.net/note/reject/91119
Search -- https://master.php.net/manage/user-notes.php