Question

PHP recently added a third argument to the is_a function. It's a boolean, and the documentation says,

If this parameter set to FALSE, string class name as object is not allowed. This also prevents from calling autoloader if the class doesn't exist.

I don't get it. I get that you can prevent calling the autoloader unnecessarily, but it doesn't prevent you passing a string as a class name:

> class A {}
> $x = new A();
> echo is_a($x, 'A', false);
1

…in fact, you can't ever pass anything but a string:

> echo is_a($x, A, false);
PHP Notice:  Use of undefined constant B - assumed 'A' in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0

Notice: Use of undefined constant B - assumed 'A' in php shell code on line 1

Call Stack:
   18.7644     625048   1. {main}() php shell code:0

1

That error message doesn't change when you change the third argument. What does string class name as object actually mean?

Était-ce utile?

La solution

The answer has to do with subclassing. In PHP 5.3.7 is_a changed so that if the first argument was not an object, PHP would __autoload that argument, effectively attempting to make it an object:

> class A {}
> class B extends A {}
> echo is_a('B', 'A');
> // nada
> echo is_a('B', 'A', true);
1

Needless to say, that can lead to some unexpected side-effects and slowdowns, so adding the third argument gives you the choice about which behavior you want.

This all probably started when someone discovered that is_a and subclass_of don't behave exactly like instanceof.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top