PHP: is_a() behavior
+
Par Remi le dimanche 25 septembre 2011, 19:42 - HowTo - Lien permanent
The is_a() function behavior have changed in latest php version. Some explanations.
In the PHP documentation is written:
bool is_a ( object $object , string $class_name )
Until php 5.3.6, if the first parameter was a string, the result wasn't really defined (we could have expect an error, at call), but it was working, and some applications were relying on this undocumented behavior (very bad idea).
$ php -r '$var="StdClass"; var_dump(is_a($var, "StdClass"));'
bool(false)
To resume : $var, is a string, so, not an object.
Since php 5.3.7, if the first paramet is a string, it is handled as a possible class name (raising autoload if needed).
$ php -r '$var="StdClass"; var_dump(is_a($var, "StdClass"));'
bool(true)
Which is the same behavior than the is_subclass_of function:
bool is_subclass_of ( mixed $object , string $class_name )
This behavior change have caused the community outcry, and a lot of impassioned discussions, notably from the PEAR team which is used to test a process return value using:
if (is_a($var,'PEAR_Error')) ...
Finally, the PHP team have decided to revert this change, and to add a new option to control this behavior.
Starting with next PHP version (5.3.9-dev, for now), the function have a new prototype, which can be described as:
bool is_a ( mixed $object , string $class_name [, bool allow_string ] )
And the result is
$ php -r '$var="StdClass"; var_dump(is_a($var, "StdClass"));'
bool(false)
$ php -r '$var="StdClass"; var_dump(is_a($var, "StdClass", false));'
bool(false)
$ php -r '$var="StdClass"; var_dump(is_a($var, "StdClass", true));'
bool(true)
I have decided to apply this fix, without any delay, in the RPM available in my repository, in version 5.3.8-5 (build running) Perhaps I will also apply it in the version available in fedora repository (see Bug #741022).
Commentaires
If you prefer the official packages, updates (version 5.3.8-3) are pending, for :