[wp-trac] [WordPress Trac] #14671: Deprecate the "accepted args" argument in add_filter() and add_action()
WordPress Trac
noreply at wordpress.org
Mon Jan 21 07:01:36 UTC 2013
#14671: Deprecate the "accepted args" argument in add_filter() and add_action()
-------------------------+-----------------------------
Reporter: markjaquith | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Future Release
Component: Plugins | Version:
Severity: normal | Resolution:
Keywords: has-patch |
-------------------------+-----------------------------
Comment (by westonruter):
I had the idea a couple years ago to use PHP's Reflection capabilities to
introspect the number of arguments defined on the function so that the use
of the `$accepted_args` 4th argument could be largely eliminated (as
5ubliminal also suggested above). At that time, WordPress still was
required to support PHP4, and so this was a dead end since Reflection was
introduced in PHP5. Now, however, since WordPress requires PHP≥5.2, it is
now safe to use Reflection and we can use it make our plugin code more
DRY. In my supplied patch, note that you can still supply the 4th
`$accepted_args` parameter, and this will override any attempt to get the
argument count via Reflection; this is useful if you are using a function
as a hook handler which has optional arguments which may get inadvertently
set when the hook is executed with extra arguments.
Regarding performance, I did a comparison of the execution time when using
Reflection vs. the baseline code that is in trunk. The test script can be
found at https://gist.github.com/4581512#file-test-php
Here is the results of the reference baseline trunk plugins.php:
{{{
$ wp eval-file test.php
Running original plugin.php
Iteration count 100000
Testing function...
2.842000 seconds
Testing method...
2.881264 seconds
Testing closure...
2.831295 seconds
}}}
And then here is the results with my patch:
{{{
$ wp eval-file test.php
Running patched plugin.php
Iteration count 100000
Testing function...
3.783207 seconds
Testing method...
3.864082 seconds
Testing closure...
3.748331 seconds
}}}
As you can see at over 100,000 iterations, the version with Reflection
runs about a third slower. I did try a version of my patch that included
static caching the Reflection calls, but it seems the logic to generate
the cache key (utilizing `spl_object_hash()`) actually is about the same
as invoking Reflection's `getNumberOfParameters` method; after repeated
tests, the version of the patch with and the version without would
consistently alternate in being faster. Nevertheless, the patch with the
cache is also available: https://gist.github.com/4581512#file-plugin-php-
alternate-patch
Personally, I think the usability benefits we gain by not having to supply
the 4th argument outweighs the overall performance hit to use Reflection
to introspect the number of arguments.
I have not yet run the patch through the unit tests.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/14671#comment:25>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list