[PHP-BUG] Req #71903 [NEW]: Introduce $offset parameter for get_defined_functions()

[email protected] ("andreas at dqxtech dot net")
Newsgroups php.standards
Message-ID <[email protected]>
From:             andreas at dqxtech dot net
Operating system: Linux
PHP version:      7.0.5RC1
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:Introduce $offset parameter for get_defined_functions()

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 bug report at https://bugs.php.net/bug.php?id=71903&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=71903&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=71903&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=71903&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=71903&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=71903&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=71903&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=71903&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=71903&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=71903&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=71903&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=71903&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=71903&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=71903&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71903&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=71903&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=71903&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=71903&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71903&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=71903&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=71903&r=mysqlcfg
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.