is_a instanceof get_class and is_subclass_of

As Denis-de-Bernardy mentioned in the Trac ticket core.trac.wordpress.org/ticket/10264, get_class does not deal with inheritance as is_a and instanceof do. To get the same functionality using get_class, is_subclass_of must also be used in the conditional test.

Because there was no appreciable difference between testing with error reporting on or off is_a instanceof and get_class, I simplified the testing by eliminating the error reporting on block. I created another emptyClass that extended the original emptyClass. I retained the is_a and instanceof tests, as well as the get_class test. But I also added an is_subclass_of test and a combined get_class or is_subclass_of test. The times shown are still for 4400000 (4 million 400 thousand) iterations. I ran two variations, the first to test if the $object 'parent-class' had the $class_name 'parent-class', and the second to test if the $object 'subclass' had the $class_name 'parent-class'.

As can be seen, using either get_class or is_subclass_of alone does not produce the correct results as an object can only be either the class or a subclass of the class, not both at the same time.

As compared to the previous test runs, is_a, instanceof and get_class had similar run times. When the $object was the 'parent-class' and had the $class_name 'parent-class', PHP had no need to test for is_subclass_of and so the run time was only a bit slower than get_class alone, being similar to the instanceof run time. But when the $object was the 'subclass' and had the $class_name 'parent-class', the run time was longer. Slower than instanceof, but still considerably faster than is_a.

$object 'parent-class', $class_name 'parent-class'

isa: true isa_off_run_time: 104.030926943
isa: true isa_off_run_time: 101.787961006
isa: true isa_off_run_time: 100.796969891

iof: true iof_off_run_time: 36.4872579575
iof: true iof_off_run_time: 36.2819008827
iof: true iof_off_run_time: 35.8666028976

gc: true gc_off_run_time: 33.9882059097
gc: true gc_off_run_time: 33.8179419041
gc: true gc_off_run_time: 33.9171960354

iso: false iso_off_run_time: 47.4339499474
iso: false iso_off_run_time: 47.8175148964
iso: false iso_off_run_time: 47.2504739761

gciso: true gciso_off_run_time: 35.2629709244
gciso: true gciso_off_run_time: 35.8787820339
gciso: true gciso_off_run_time: 34.9580559731

$object 'subclass', $class_name 'parent-class'

isa: true isa_off_run_time: 102.17751193
isa: true isa_off_run_time: 101.644783974
isa: true isa_off_run_time: 102.215440989

iof: true iof_off_run_time: 36.0798051357
iof: true iof_off_run_time: 35.9090869427
iof: true iof_off_run_time: 36.0413579941

gc: false gc_off_run_time: 33.3235809803
gc: false gc_off_run_time: 33.2563240528
gc: false gc_off_run_time: 33.2467691898

iso: true iso_off_run_time: 48.7431797981
iso: true iso_off_run_time: 48.9224038124
iso: true iso_off_run_time: 48.5773239136

gciso: true gciso_off_run_time: 78.766422987
gciso: true gciso_off_run_time: 79.0953769684
gciso: true gciso_off_run_time: 79.1366560459

Although using a combined get_class or is_subclass_of in place of is_a results in more-or-less improved performance, the conditional is not as readable. My primary reason for the code substitution is to eliminate the E_STRICT deprecated errors from my error logs without using the instanceof operator and the problems it's use might introduce.

Technorati Tags: ,

Post a Comment

Your email is never shared. Required fields are marked *

*
*