Calling the parent method of an inherited class from the base class

The following example does not work, because when called parentin a class, Aphp looks for the parent class of the class A, but it does not exist. I would prefer this line to call Test()in the class B.

Is it possible?

(I know this seems like a dumb example, but it has a practical application)

abstract class A {
    function CallParentTest()
    {
        return call_parent_method('Test');
    }
}

abstract class B extends A {
    function Test()
    {
        return 'test passed';
    }
}

class C extends B {
    function Test()
    {
        return $this->CallParentTest();
    }
}

$object = new C();
echo $object->Test();

Thank!

EDIT
I changed the keyword parentto the compilation method call_parent_method, because I think it may have confused people. I know that there is no way to do this using the keyword.

, , . B::Test() , .

+5
3

. ReflectionMethod , . , setAccessible(true), .

class A {
    public function bypassOverride() {
        echo "Hi from A\n";
        $r = new ReflectionMethod('B', 'override');
        $r->invoke($this);
    }
}

class B extends A {
    public function override() {
        echo "Hi from B\n";
    }
}

class C extends B {
    public function override() {
        echo "Hi from C\n";
        $this->bypassOverride();
    }
}

$c = new C;
$c->override();

Hi from C
Hi from A
Hi from B

bypassOverride() , .

+6

?

.

parent, . - , . , , , .

+1

webbiedave parent, , Template Method, , . , .

abstract class ExceptionIgnorer {
    public function doIt() {
        try {
            $this->actuallyDoIt();
        }
        catch (Exception $e) {
            // ignore the problem and it might go away...
        }
    }

    public abstract function actuallyDoit();
}

class ErrorThrower extends ExceptionIgnorer {
    public function actuallyDoIt() {
        throw new RuntimeException("This will be ignored");
    }
}

$thrower = new ErrorThrower;
$thrower->doIt(); // no problem

doIt() - , , .

+1

All Articles