Quantcast

Use of is_a()

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Use of is_a()

cadorn
Regarding issue: http://framework.zend.com/issues/browse/ZF-10526

What is the consensus on using is_a() instead of 'instanceof'.

is_a() throws a deprecation notice for PHP < 5.3.

I don't want to use instanceof as I don't think the class needs to be
loaded just for that one check.

Should I be using a combination of get_class() and is_subclass_of() to
replace is_a()?

Thanks
Christoph

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use of is_a()

Giorgio Sironi
instanceof does not load a class, at least if it does not exist:

<?php
$object = new stdClass;
var_dump($object instanceof ThisClassDoesNotExist);

[20:23:49][giorgio@Tony:~]$ php instanceof.php
bool(false)


--
Giorgio Sironi
Piccolo Principe & Web Engineer
http://giorgiosironi.blogspot.com
http://twitter.com/giorgiosironi
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use of is_a()

weierophinney
Administrator
In reply to this post by cadorn
-- Christoph Dorn <[hidden email]> wrote
(on Monday, 11 October 2010, 11:05 AM -0700):

> Regarding issue: http://framework.zend.com/issues/browse/ZF-10526
>
> What is the consensus on using is_a() instead of 'instanceof'.
>
> is_a() throws a deprecation notice for PHP < 5.3.
>
> I don't want to use instanceof as I don't think the class needs to
> be loaded just for that one check.
>
> Should I be using a combination of get_class() and is_subclass_of()
> to replace is_a()?

instanceof does not require that the class referenced be loaded; it
simply checks to see if the class of the object matches it. (Search for
"instanceof Zend_Config" throughout the codebase -- most cases where we
match against it, the class is never required, since it's not explicitly
used.)

Additionally, instanceof checks inheritance, and will return true when
the object:

 * implements an interface (if an interface name is given as the
   argument)
 * matches any class in the inheritance (extends) tree

It's exactly the functionality you want to use here.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use of is_a()

cadorn

On 10-10-11 11:40 AM, Matthew Weier O'Phinney wrote:
> instanceof does not require that the class referenced be loaded; it
> simply checks to see if the class of the object matches it. (Search for
>    

Ah, I guess I have been thinking in terms of JavaScript too much lately.
I'll commit a fix.

Thanks
Christoph

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use of is_a()

Julien Pauli-2
is_a(), is_subclass_of() and instanceof don't call autoload nor need
the class to exist since PHP 5.1.0

J.Pauli

On Mon, Oct 11, 2010 at 9:13 PM, Christoph Dorn
<[hidden email]> wrote:

>
> On 10-10-11 11:40 AM, Matthew Weier O'Phinney wrote:
>>
>> instanceof does not require that the class referenced be loaded; it
>> simply checks to see if the class of the object matches it. (Search for
>>
>
> Ah, I guess I have been thinking in terms of JavaScript too much lately.
> I'll commit a fix.
>
> Thanks
> Christoph
>
>
Loading...