[PHP] wrong behaviour with is_subclass_of() ??
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 I I would expect the following output: A implements I B implements I Johannes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] wrong behaviour with is_subclass_of() ??
On 19 Sep 2008, at 15:19, Johannes Müller wrote: 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 I I would expect the following output: A implements I B implements I Because there is a big difference between extends and implements, one of which being that the class is not considered to be a subclass of an interface it implements. Seems entirely logical to me. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] wrong behaviour with is_subclass_of() ??
Stut wrote: outputs: B implements I I would expect the following output: A implements I B implements I Because there is a big difference between extends and implements, one of which being that the class is not considered to be a subclass of an interface it implements. Seems entirely logical to me. But B is also no subclass of I - it just implements I as well as A. So there could be two possible straight solutions: 1. Neither A nor B is a subclass of I. 2. Both A and B are subclasses of I. Johannes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] wrong behaviour with is_subclass_of() ??
On 19 Sep 2008, at 15:58, Johannes Mueller wrote: Stut wrote: outputs: B implements I I would expect the following output: A implements I B implements I Because there is a big difference between extends and implements, one of which being that the class is not considered to be a subclass of an interface it implements. Seems entirely logical to me. But B is also no subclass of I - it just implements I as well as A. So there could be two possible straight solutions: 1. Neither A nor B is a subclass of I. 2. Both A and B are subclasses of I. A implements I therefore A *is not* a subclass of I. B extends I therefore B *is* a subclass of I. In the case of A it's simply stating that it implements every method defined by I. B on the other hand does not necessarily implement the methods defined in I, but those methods will still be available on instances of B but the code in I will be used. I don't see what's difficult to understand here. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] wrong behaviour with is_subclass_of() ??
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 I because 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] wrong behaviour with is_subclass_of() ??
Jochem Maas wrote: B implements I because B subclasses A and A implements I, I is not a base class. try the experiment with is_a() instead. This was my starting point and is_subclass_of() was a sub-ordinate target, because i needed it on the class-side of life and not the instantiated way. 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() class_implements() would solve all these problems were well! specifically looks at base classes (extends), I'm a little surprised that it even returns true for interfaces at all (implements). And this was the reason where it stops to make sense for me. And if you hadn't pointed this out, this would have been in my next email. Johannes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] wrong behaviour with is_subclass_of() ??
Johannes Mueller schreef: Jochem Maas wrote: B implements I because B subclasses A and A implements I, I is not a base class. try the experiment with is_a() instead. This was my starting point and is_subclass_of() was a sub-ordinate target, because i needed it on the class-side of life and not the instantiated way. 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() class_implements() would solve all these problems were well! specifically looks at base classes (extends), I'm a little surprised that it even returns true for interfaces at all (implements). And this was the reason where it stops to make sense for me. And if you hadn't pointed this out, this would have been in my next email. I think the reason for the current behaviour is that interface can be seen as an 'is a' relationship. is_subclass_of() looks specifically for an 'is a' relationship with regard to whatever is above in the class heriarchy ... there is nothing above A so it will always return false for A. so it's a little odd, but logical at the same time ... now the namespaces implementation as it currently stands ... that's a little psychotic :-) Johannes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php