Req #71903 [Com]: Introduce $offset parameter for get_defined_functions()
[email protected] ("andreas at dqxtech dot net")
| Newsgroups | php.standards |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=71903&edit=1
ID: 71903
Comment by: andreas at dqxtech dot net
Reported by: andreas at dqxtech dot net
Summary: Introduce $offset parameter for
get_defined_functions()
Status: Open
Type: Feature/Change Request
Package: PHP Language Specification
Operating System: Linux
PHP Version: 7.0.5RC1
Block user comment: N
Private report: N
New Comment:
One flaw of this proposal is that the return value of get_defined_functions() would change from 'string[]' to 'string[][]|string[]'. Which of course is still 'array' === 'array'.
Stable return types are generally preferable.
A solution could be to have a separate function, e.g. get_user_defined_functions().
I will leave this argument to others.
Previous Comments:
------------------------------------------------------------------------
[2016-03-25 20:04:37] andreas at dqxtech dot net
Description:
------------
Some frameworks / CMSes (mostly Drupal, maybe others) rely on function naming patterns for their internal functionality.
Discovery is often implemented with a lot of function_exists($module . '_' . $hook), or even function_exists($module . '_' . $hook . '_' . $suffix) or similar, for a lot of $module + $hook (+ $suffix) combinations. This can be quite costly.
An alternative is to iterate over get_defined_functions()['user'], and analyze each function for the patterns it matches.
A problem with this is that new functions can appear when new files are included. get_defined_functions() is cheap when called once in a request, but calling it repeatedly can be costly (around ~2ms each call, but depends on the project).
The list of returned functions is already ordered by definition time. New functions are towards the end of the list. Hence, it is possible to distinguish newly added functions by keeping track of count(get_defined_functions()['user']), when calling it repeatedly. But the cost still adds up.
I imagine that get_defined_functions() would be faster if it could be called with an $offset parameter, so it would only return the functions from this offset onwards.
The ['internal'] functions do not really change during a request, so I think with this parameter set, we only need ['user'] functions.
Following another request, https://bugs.php.net/bug.php?id=51855, the parameter could be set to 'internal' or 'user' to specify one of the arrays.
Test script:
---------------
function f0() {}
function f1() {}
function f2() {}
function f3() {}
assert(get_defined_functions()['user'] === array('f0', 'f1', 'f2', 'f3'));
assert(get_defined_functions('user') === array('f0', 'f1', 'f2', 'f3'));
assert(get_defined_functions(0) === array('f0', 'f1', 'f2', 'f3'));
assert(get_defined_functions(1) === array('f1', 'f2', 'f3'));
assert(get_defined_functions(3) === array('f3'));
assert(get_defined_functions(4) === array());
assert(get_defined_functions(5) === array());
Expected result:
----------------
All assertions pass. No warnings or errors.
Actual result:
--------------
https://3v4l.org/q68fI
Warning: get_defined_functions() expects exactly 0 parameters, 1 given in /in/q68fI on line 8
Warning: assert(): assert(get_defined_functions('user') === ['f0', 'f1', 'f2', 'f3']) failed in /in/q68fI on line 8
Warning: get_defined_functions() expects exactly 0 parameters, 1 given in /in/q68fI on line 9
Warning: assert(): assert(get_defined_functions(0) === ['f0', 'f1', 'f2', 'f3']) failed in /in/q68fI on line 9
Warning: get_defined_functions() expects exactly 0 parameters, 1 given in /in/q68fI on line 10
Warning: assert(): assert(get_defined_functions(1) === ['f1', 'f2', 'f3']) failed in /in/q68fI on line 10
Warning: get_defined_functions() expects exactly 0 parameters, 1 given in /in/q68fI on line 11
Warning: assert(): assert(get_defined_functions(3) === ['f3']) failed in /in/q68fI on line 11
Warning: get_defined_functions() expects exactly 0 parameters, 1 given in /in/q68fI on line 12
Warning: assert(): assert(get_defined_functions(4) === []) failed in /in/q68fI on line 12
Warning: get_defined_functions() expects exactly 0 parameters, 1 given in /in/q68fI on line 13
Warning: assert(): assert(get_defined_functions(5) === []) failed in /in/q68fI on line 13
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71903&edit=1