Req #70507 [Opn]: Combination of is_file() + include (e.g. "include_if_exists")
| From: | requinix@php.net | Date: | Tue, 15 Sep 2015 20:08:13 +0000 |
| Subject: | Req #70507 [Opn]: Combination of is_file() + include (e.g. "include_if_exists") | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-196021@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70507&edit=1
ID: 70507
Updated by: requinix@php.net
Reported by: andreas at dqxtech dot net
Summary: Combination of is_file() + include (e.g.
"include_if_exists")
Status: Open
Type: Feature/Change Request
-Package: PHP Language Specification
+Package: Scripting Engine problem
Operating System: Linux
PHP Version: 7.0.0RC2
Block user comment: N
Private report: N
New Comment:
For consistency there would have to be two functions: include_if_exists and include_once_if_exists.
And files executed through those would not be able to use "return" to return values to the
caller; having the functions an object would be a jarring departure from how the others work.
If you're concerned about how include/etc. do their own file lookups then wouldn't it be
better to fix that specifically? PHP has a file stat cache already where the result of the first
lookup is reused throughout execution (unless the cache gets cleared). I did some quick strace tests
and it doesn't look like the functions are using it...
Previous Comments:
------------------------------------------------------------------------
[2015-09-15 18:03:06] 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);
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70507&edit=1