note 103548 added to function.spl-autoload
| From: | LukeScott at osu1 dot php dot net | Date: | Wed, 20 Apr 2011 02:03:08 +0000 |
| Subject: | note 103548 added to function.spl-autoload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178981@lists.php.net to get a copy of this message | ||
If you want to make the best use out of autoload with an APC cache don't use spl_autoload. It
uses relative paths and thus will perform a stat even with apc.stat=0 (either that, or it
doesn't work at all).
Instead make a custom function and use require/include with an absolute path (register it with
spl_autoload_register).
Do NOT use *_once functions or a relative path. This will fail harder than spl_autoload.
Also avoid using file_exists and is_file. This will also perform a stat.
Why are stats bad? Because they access the file system. PHP does have a stat cache that helps, but
it defeats the purpose of apc.stat = 0.
It's also good to keep in mind that it's good to keep your custom autoload function
simple. This is my Loader class:
<?php
class Loader
{
public static function registerAutoload()
{
return spl_autoload_register(array(__CLASS__, 'includeClass'));
}
public static function unregisterAutoload()
{
return spl_autoload_unregister(array(__CLASS__, 'includeClass'));
}
public static function includeClass($class)
{
require(PATH . '/' . strtr($class, '_\\', '//') . '.php');
}
}
?>
Also want to point out that APC does an optimization with require/include (not *_once) with relative
paths if require/include is done in the global scope (and isn't conditional). So it would be a
good idea to explicitly include files you know you're going to use on every request (but
don't use *_once). You could, for example, add a "registerProfiledAutoload" to the
above class and keep track of what you're including to help you determine what you could
explicitly include (during development, not production). The key is try not to make heavy use out of
autoload.
If you must use relative paths and don't care about having to lower-case your file-names then
spl_autoload works great.
----
Server IP: 64.71.164.2
Probable Submitter: 74.93.181.202
----
Manual Page -- http://www.php.net/manual/en/function.spl-autoload.php
Edit -- https://master.php.net/note/edit/103548
Del: integrated -- https://master.php.net/note/delete/103548/integrated
Del: useless -- https://master.php.net/note/delete/103548/useless
Del: bad code -- https://master.php.net/note/delete/103548/bad+code
Del: spam -- https://master.php.net/note/delete/103548/spam
Del: non-english -- https://master.php.net/note/delete/103548/non-english
Del: in docs -- https://master.php.net/note/delete/103548/in+docs
Del: other reasons-- https://master.php.net/note/delete/103548
Reject -- https://master.php.net/note/reject/103548
Search -- https://master.php.net/manage/user-notes.php