Is there a specific benefit of using instanceof instead of === null before initializing objects in php.
I see code like this all the time in the Zend framework:
if (!self::$_httpClient instanceof Zend_Http_Client) {
/**
* @see Zend_Http_Client
*/
#require_once 'Zend/Http/Client.php';
self::$_httpClient = new Zend_Http_Client();
}
return self::$_httpClient;
But the property cannot be set externally and will therefore be null until set
Its correct you can do it with your solution and the "===" but then you cannot check wheather the instance is really an instance of "Zend_Http_Client" perhaps you call another class to $_httpClient then its not NULL and its not working.
In my mind its better to use the internal functions of php to check such things.
This looks like a singleton implementation. If that's true there is no good reason for this check - checking for null
would be fine and more logical.
The check is a little more definitive if you absolutely want self::$_httpClient to be a Zend_Http_Client and not anything else.
Although good programming may prevent it from being anything else, if the variable were to become a String, int, or an instance of another object it would be corrected where checking for null would not.
You can extend nearly any class in ZF. Therefore you can never be sure, that the child class would not replace setHttpClient() with different implementation. Also you can't be sure that someone would not like to use his own HTTP client implementation. With this code, you check for both at once. If it's null it works. Also when it's a class that does not extend Zend_Http_Client, it will also work.