|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-09-15 18:03 UTC] andreas at dqxtech dot net
Description:
------------
For slightly improved class loader performance, it would be nice to have a combination of is_file() + include, as a new language construct.
Currently, include raises a warning if the file does not exist, is not accessible, or is not a file.
This means that a class loader cache that is not sure if the file exists needs to call is_file() before include. And to be strict, it would also have to call is_readable().
The new language construct would include the file if it exists, and return either TRUE or FALSE to indicate the success. This way the script does not need to hit the filesystem more than once.
An explicit return value of the file can be ignored, because this is mostly designed for class files.
There could be some more interesting behavior on failure. E.g. instead of just returning FALSE, it could return an object with an indication what went wrong.. Or this information could be retrieved from elsewhere.
The goal is to optimize the performance in case of success, not on failure.
-------
This being said: Maybe this is all not necessary, if the compiler/optimizer can automatically merge the is_file() and the include.
Test script:
---------------
spl_autoload_register('my_autoload_callback');
function my_autoload_callback($class) {
$file = my_autoload_cache_lookup($class);
if (!$file) {
return;
}
if (true === include_if_exists $file) {
// Done with the classloader.
return;
}
my_autoload_cache_unset($class);
$file = my_autoload_psr4_lookup($class);
if (!$file) {
return;
}
if (true === include_if_exists $file) {
my_autoload_cache_set($class, $file);
}
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 07:00:01 2025 UTC |
> Someone will tell me I shouldn't. I am sure of that :) Right. Generally speaking, it is advised. "@" operator is like "goto" for me. Sloppy use of "goto" ruins code, but decent use of "goto" achieves clean and robust code. if (@include($script)) { // ERROR!! } is good use case of "@" IMO.if (@include($script)) { should be if (!@include($script)) {