use self::build() or A::build() for that case

2006/8/4, Christian Weiske <[EMAIL PROTECTED]>:

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




Reply via email to