Hi Hans,

On Tue, 17 Feb 2004 15:44:38 -0500, Hans Lellelid wrote:

> Ok, I guess that's just how it is in PHP.  The only thing I would cling
> to is that this behavior is inconsistent with the way classes behave:
> i.e. you can override a method in a class and the ($obj instanceof
> ParentClass) will return still return true.
I got your point, and you're right! PHP is inconsistent when it accepts
overriding(with different signature) and still recognizes the class as
"instance of" the parent.
The inheritance is broken because of the lack of overloading. See this
example:

class A {
    function init() { echo "A::init"; }
    function doSomething($arg1, $arg2) { echo "A::doSomething"; }
}
                                                                                
class B extends A {
    function doSomething($arg1, $arg2, $arg3, $arg4 = null) {
         echo "B::doSomething"; 
    }
    function doSomethingElse($arg1) { echo "B::doSomethingElse"; }
}
class C {
    static function acceptsA(A $a) { $a->doSomething(1, 2); }
}
$a = new A();
$b = new B();
echo ($b instanceof A) ? "YES" : "NO", "\n";
echo "trying A = ", C::acceptsA($a), "\n";
echo "trying B = ", C::acceptsA($b), "\n";

This example reports that B is an instance of A, but B doesn't implement
the signature of A(because it got overriden) and then the last line
generates an error. So, it's very odd, but technically B isn't an
instance of A even if it inherits from A.

> I think that overriding with default arguments would get around 90% of
> the cases where this is a problem.  It won't get around my specific
> case, but I can live with just not using an OO hierarchy for my code.
Agree. With default arguments, the inheritance isn't broken.

> I still think that interfaces should follow the same behavior as classes
> (i.e. if you can override in classes you can override in interfaces),
> but I understand that academically this is wrong.  Of course overriding
> is wrong period, but it's the route you have to take when overloading
> isn't an option.

IMHO, this odd behavior exists because of the lack of overloading support.
To be consistent, PHP shouldn't allow the overriding of methods with
different signatures or register, at parse time, that the child break the
inheritance for each parent that has methods overriden with different
signatures. 

Best Regards,

Cristiano Duarte

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to