note 111875 added to function.spl-autoload-register
| From: | a dot schaffhirt at sedna-soft dot de | Date: | Sun, 07 Apr 2013 13:42:57 +0000 |
| Subject: | note 111875 added to function.spl-autoload-register | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194369@lists.php.net to get a copy of this message | ||
What I said here previously is only true on Windows. The built-in default autoloader that is
registered when you call spl_autoload_register() without any arguments simply adds the qualified
class name plus the registered file extension (.php) to each of the include paths and tries to
include that file.
Example (on Windows):
include paths:
- "."
- "d:/projects/phplib"
qualified class name to load:
network\http\rest\Resource
Here's what happens:
PHP tries to load
'.\\network\\http\\rest\\Resource.php'
-> file not found
PHP tries to load
'd:/projects/phplib\\network\\http\\rest\\Resource.php'
-> file found and included
Note the slashes and backslashes in the file path. On Windows this works perfectly, but on a Linux
machine, the backslashes won't work and additionally the file names are case-sensitive.
That's why on Linux the quick-and-easy way would be to convert these qualified class names to
slashes and to lowercase and pass them to the built-in autoloader like so:
<?php
spl_autoload_register(
function ($pClassName) {
spl_autoload(strtolower(str_replace("\\", "/", $pClassName)));
}
);
?>
But this means, you have to save all your classes with lowercase file names. Otherwise, if you omit
the strtolower call, you have to use the class names exactly as specified by the file name, which
can be annoying for class names that are defined with non-straightforward case like e. g.
XMLHttpRequest.
I prefer the lowercase approach, because it is easier to use and the file name conversion can be
done automatically on deploying.
----
Server IP: 69.147.83.199
Probable Submitter: 95.91.69.19
----
Manual Page -- http://php.net/manual/en/function.spl-autoload-register.php
Edit -- https://master.php.net/note/edit/111875
Del: integrated -- https://master.php.net/note/delete/111875/integrated
Del: useless -- https://master.php.net/note/delete/111875/useless
Del: bad code -- https://master.php.net/note/delete/111875/bad+code
Del: spam -- https://master.php.net/note/delete/111875/spam
Del: non-english -- https://master.php.net/note/delete/111875/non-english
Del: in docs -- https://master.php.net/note/delete/111875/in+docs
Del: other reasons-- https://master.php.net/note/delete/111875
Reject -- https://master.php.net/note/reject/111875
Search -- https://master.php.net/manage/user-notes.php