Is it possible to cancel function override in parent class and use function from top level parent

Posted by Anatoliy Gusarov on Stack Overflow See other posts from Stack Overflow or by Anatoliy Gusarov
Published on 2013-11-06T09:30:49Z Indexed on 2013/11/06 9:53 UTC
Read the original article Hit count: 169

Filed under:
|
|
|
|
class TopParent
{
    protected function foo()
    {
        $this->bar();
    }

    private function bar()
    {
       echo 'Bar';
    }
}

class MidParent extends TopParent
{
    protected function foo()
    {
        $this->midMethod();
        parent::foo();
    }

    public function midMethod()
    {
        echo 'Mid';
    }

    public function generalMethod()
    {
       echo 'General';
    }
}

Now the question is if I have a class, that extends MidParent because I need to call

class Target extends MidParent
{
    //How to override this method to return TopParent::foo(); ?
    protected function foo()
    {
    }
}

So I need to do this:

$mid = new MidParent();
$mid->foo(); // MidBar
$taget = new Target();
$target->generalMethod(); // General
$target->foo(); // Bar

UPDATE Top parent is ActiveRecord class, mid is my model object. I want to use model in yii ConsoleApplication. I use 'user' module in this model, and console app doesn't support this module. So I need to override method afterFind, where user module is called. So the Target class is the class that overrides some methods from model which uses some modules that console application doesn't support.

© Stack Overflow or respective owner

Related posts about php

Related posts about oop