is_a instanceof and get_class

One of the more common errors thrown by the WordPress Core files are the E_STRICT errors:

is_a(): Deprecated. Please use the instanceof operator

These errors are somewhat unique. The instanceof operator was not introduced until PHP version 5.0 and as of version 5.3 is_a is no longer deprecated. Since is_a works in all PHP versions >= 4.2 (and in fact, in some versions instanceof can throw a fatal error), should these errors simply be ignored?

Other than keeping these errors from cluttering up error logs when error reporting is configured to include them, might there be a good reason for changing the WordPress files to use instanceof instead? Does an error being thrown slow things down even when error reporting is off?

To test this, I put together a simple benchmark script and ran several variations of it. In all tests I turned off display errors, created an emptyClass, assigned an istance of that class to a variable, set an iteration variable to 4400000 (4 million 400 thousand), and initialized some variables to hold the conditional tests' results. I then created 2 test blocks, one with error reporting on, the other off. In each of these were 3 test blocks where the time limit was set to 120 seconds, microtime was assigned to a start variable, a for loop with a conditional test was used, microtime was assigned to a stop variable, start time subtracted from stop time, and the resulting values displayed to the screen.

I used 3 conditional tests in ternary form, in most tests using a variable to hold the string "emptyClass" and assigning either the string "true" or "false". But in other tests, I used the string in the conditional [test runs 5,6], and in the last assigned boolean true or false [test run 6]. In some tests I assigned a different emptyClass to "foo" [test runs 3,4]. I also changed the sequence of the error reporting "on" and "off" blocks a few times.

The conditional tests used were:
$isa = (is_a($foo, 'emptyClass')) ? true : false;
$iof = ($foo instanceof emptyClass) ? true : false;
$gc = (get_class($foo) == 'emptyClass') ? true : false;

The results were:

isa: true isa_off_run_time: 100.178678036
isa: true isa_off_run_time: 99.1540658474
isa: false isa_off_run_time: 99.960310936
isa: false isa_off_run_time: 103.339380026
isa: true isa_off_run_time: 106.143660069
isa: 1 isa_off_run_time: 105.840899944

iof: true iof_off_run_time: 35.3434741497
iof: true iof_off_run_time: 35.5949220657
iof: false iof_off_run_time: 35.5492649078
iof: false iof_off_run_time: 35.5479259491
iof: true iof_off_run_time: 34.432945013
iof: 1 iof_off_run_time: 33.126625061

gc: true gc_off_run_time: 33.4457361698
gc: true gc_off_run_time: 33.5602648258
gc: false gc_off_run_time: 33.3188569546
gc: false gc_off_run_time: 34.7413768768
gc: true gc_off_run_time: 33.0190939903
gc: 1 gc_off_run_time: 31.8450849056

isa: true isa_on_run_time: 102.209348202
isa: true isa_on_run_time: 102.416124105
isa: false isa_on_run_time: 100.420098066
isa: false isa_on_run_time: 102.395892143
isa: true isa_on_run_time: 106.603455782
isa: 1 isa_on_run_time: 104.104444027

iof: true iof_on_run_time: 35.7016470432
iof: true iof_on_run_time: 35.5722339153
iof: false iof_on_run_time: 35.5262870789
iof: false iof_on_run_time: 35.2299501896
iof: true iof_on_run_time: 34.4370470047
iof: 1 iof_on_run_time: 32.9595680237

gc: true gc_on_run_time: 33.9093399048
gc: true gc_on_run_time: 33.8212268353
gc: false gc_on_run_time: 33.8323700428
gc: false gc_on_run_time: 33.9559469223
gc: true gc_on_run_time: 32.9235389233
gc: 1 gc_on_run_time: 31.7332019806

As can be seen, I found no appreciable difference between the error reporting off and error reporting on run times. However, is_a takes roughly 3 times as long as instanceof, and get_class is somewhat faster than instanceof.

Does this matter? Some files that call is_a run only very seldom, and some may not get called at all depending on your blog set up and your blogging habits. But, as we shall see, at other times is_a is called many times.

These tests provide only a very limited result set. Because of variations due to other running system processes, and the changes made to the script between test runs, they should be interpreted with caution. If you care to, you should run similar tests on your own server with your version of PHP.

Although the error message suggests using the instanceof operator insread of is_a, the instanceof operator was not introduced until PHP version 5.0, and as mentioned, in some cases can throw a fatal error. get_class however, was introduced in PHP version 4.0 and also appears to have the fastest run time of the 3 conditionals, even if it is slightly more verbose.

I have left a comment regarding this on the Trac ticket core.trac.wordpress.org/ticket/10264

Technorati Tags: ,

Post a Comment

Your email is never shared. Required fields are marked *

*
*