is_a (WAS: Re: instanceof / __CLASS__ question [SOLVED])

From: Date: Tue, 31 Aug 2004 20:47:29 +0000
Subject: is_a (WAS: Re: instanceof / __CLASS__ question [SOLVED])
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-195338@lists.php.net to get a copy of this message
Greg Beaver wrote:
Hi, I'm not sure I understand why you would use instanceof on $this. Try this code:
Kind of complicated (so probably not the best way) but I have objects I'm creating with a factory method - and all of these objects *should* be extending a parent class. It's in the parent class where I'm using $this instanceof __CLASS__ and alternatives are welcome. I thought is_a was being deprecated (correct me if I'm wrong) ?
instanceof defines whether an object is or descends from a class (like is_a()). If you want to check whether $this is the Test class or a descendant class, the simplest way I know is if (get_class($this) == 'Test')
Yes, it probably makes more sense to do it this way. Reflections would be overkill, all I need is to test for inheritance.

« previous php.general (#195338) next »