Re: Method inheritance and parent classes

From: Date: Sat, 05 Aug 2006 06:12:59 +0000
Subject: Re: Method inheritance and parent classes
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-25204@lists.php.net to get a copy of this message
use self::build() or A::build() for that case 2006/8/4, Christian Weiske <cweiske@cweiske.de>:
Hello all, I recently had a problem that could be solved, but in an ugly way: Class B extended Class A. Both classes define a method with the same name. B called A's constructor, and A called $this->method(). This executed B::method(), not A::method() as you would expect it. <?php class A {
    function __construct() {
        $this->build();
    }
    function build() {
        echo "buildA ";
    }
} class B extends A {
    function __construct() {
        parent::__construct();
        $this->build();
    }
    function build() {
        echo "buildB ";
    }
} new B(); ?> output is: "buildB buildB " expected would be: "buildA buildB " I suggest changing the behavior of php to never call methods of classes that are added by subclasses, only own ones or inherited ones. -- Regards/MfG, Christian Weiske


« previous php.internals (#25204) next »