is_a Conclusion

Errors:

For the majority of WordPress users, the use of is_a in the core files is of little concern. The E_STRICT errors, in PHP versions >= 5.0 < 5.3 only, can be safely ignored and are not a problem if error reporting does not include them.

Performance:

For those that have PHP versions >= 5.0 and desire a slight improvement in performance, changing the code to use the instanceof operator, at least in the wp-includes/classes.php file, may be worth the effort. In my crude tests is_a ran in .000023 seconds and instanceof in .0000082 seconds. As the is_wp_error() function is called hundreds of times per page load, this one edit will amount to a saving of roughly .0074 seconds per page load.

Readability:

The use of either is_a or instanceof in conditional tests is readable and it is easy to comprehend the intent. My use of get_class and is_subclass_of, although supported in all versions of PHP, not so much.

Maintainability:

Considering how often new versions of WordPress are released — 2 more in the short time since this series of posts began — making 38 edits in 15 files just to replace the use of is_a with something else could become a chore.

To summarize, several things need to be considered before making any edits to the WordPress core files:

  • Are the E_STRICT errors important?
  • What version of PHP is the server running?
  • How important is performance?
  • How important is code readability?
  • How important is maintainability?
Technique	PHP	Speed	Errors		Readability

is_a		4,5	slow	Strict*		simple
instanceof	5	fast	$var fix**	simple
get_class
is_subclass_of	4,5	mid***	$var fix**	complex

* is_a is only an E_STRICT error for PHP versions >= 5.0 < 5.3 ** Both instanceof and is_subclass_of can cause fatal errors in PHP versions 5.0 < 5.1 unless a work-around is used, in my examples, by using a variable to hold the class name. *** When get_class alone is enough, the conditional approximates the speed of instanceof, but when PHP must test for is_subclass_of the speed is closer to, but a bit faster than, that of is_a

Choices for is a:

is_a($object, 'WP_Class')

( $object instanceof WP_Class )

$class_name = 'WP_Class';
( $object instanceof $class_name )

( ( get_class($object) == 'WP_Class' )
	 || is_subclass_of($object, 'WP_Class') )

$class_name = 'WP_Class';
( ( get_class($object) == 'WP_Class' )
	 || is_subclass_of($object, $class_name) )

Choices for Not is a:

!is_a($object, 'WP_Class')

!( $object instanceof WP_Class )

$class_name = 'WP_Class';
!( $object instanceof $class_name )

( ( get_class($object) != 'WP_Class' )
	 && !is_subclass_of($object, 'WP_Class') )

$class_name = 'WP_Class';
( ( get_class($object) != 'WP_Class' )
	 && !is_subclass_of($object, $class_name) )

WordPress core files containing is_a

Technorati Tags: ,