Note that, the orders of file extensions is important for
performance. You should make the priority of your favourite
file extension higest or use only one extension for your
class files. Check out this example:
Some class files:
ClassA.php
<?php class ClassA { var $val = 'Hello from class
"ClassA"'; } ?>
ClassB.php
<?php class ClassB { var $val = 'Hello from class
"ClassB"'; } ?>
ClassC.php
<?php class ClassC { var $val = 'Hello from class
"ClassC"'; } ?>
ClassD.php
<?php class ClassD { var $val = 'Hello from class
"ClassD"'; } ?>
ClassE.php
<?php class ClassE { var $val = 'Hello from class
"ClassE"'; } ?>
1. Simple:
<?php
// default priority: .inc .php
for($n=65; $n<70; $n++) {
$className = 'Class'.chr($n);
spl_autoload($className);
$ins = new $className;
echo $ins->val.'<br>';
}
// 4.2 miliseconds
?>
2. Change priority:
<?php
spl_autoload_extensions('.php,.inc');
// new priority: .php .inc
for($n=65; $n<70; $n++) {
$className = 'Class'.chr($n);
spl_autoload($className);
$ins = new $className;
echo $ins->val.'<br>';
}
// 1.4 miliseconds
?>
Or you can use this simple function that runs a bit faster
for the extensions with lower priority
<?php
function my_autoload($className, $extList='.inc,.php') {
$ext = explode(',',$extList);
foreach($ext as $x) {
$fname = $className.$x;
if( file_exists($fname)) {
require_once($fname);
return true;
}
}
return false;
}
for($n=65; $n<70; $n++) {
$className = 'Class'.chr($n);
my_autoload($className);
$ins = new $className;
echo $ins->val.'<br>';
}
// 2.6 miliseconds
?>
---
Safak Ozpinar - Istanbul University, Computer Engineering
----
Server IP: 69.147.83.197
Probable Submitter: 81.215.143.171
----
Manual Page -- http://www.php.net/manual/en/function.spl-autoload.php
Edit -- https://master
.php.net/note/edit/78053
Del: integrated -- h
ttps://master.php.net/note/delete/78053/integrated
Del: useless -- http
s://master.php.net/note/delete/78053/useless
Del: bad code -- htt
ps://master.php.net/note/delete/78053/bad+code
Del: spam -- https:/
/master.php.net/note/delete/78053/spam
Del: non-english --
https://master.php.net/note/delete/78053/non-english
Del: in docs -- http
s://master.php.net/note/delete/78053/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78053
Reject -- https://mast
er.php.net/note/reject/78053
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|