Re: wrong behaviour with is_subclass_of() ??
| From: | Jochem Maas | Date: | Fri, 19 Sep 2008 15:53:34 +0000 |
| Subject: | Re: wrong behaviour with is_subclass_of() ?? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-280513@lists.php.net to get a copy of this message | ||
Johannes Müller schreef:
Why does the following code <?php interface I { public function a(); } class A implements I { public function a() {} } class B extends A { } if (is_subclass_of('A', 'I')) echo "A implements I\n"; if (is_subclass_of('B', 'I')) echo "B implements I\n"; ?> outputs: B implements Ibecause B subclasses A and A implements I, I is not a base class. try the experiment with is_a() instead. also you should preferablly use the instanceof syntax: <?php $a = new A; $b = new B; if ($a instanceof I)) echo "A implements I\n"; if ($b instanceof I)) echo "B implements I\n"; or use the return data from class_implements(), is_subclass_of() specifically looks at base classes ("extends"), I'm a little surprised that it even returns true for interfaces at all ("implements").
I would expect the following output: A implements I B implements I Johannes