Quiet parent method call behind me

Possible duplicate:
Force parent method call

I have a class like this

abstract class theme{

  abstract function header(){
    // do stuff here
  }

  abstract function footer(){
    // do stuff here

  }

}

therefore, all child classes must have these 2 methods:

class Atheme extends theme{

  function header(){
    // do stuff here

    echo ..
  }

  function footer(){
    // do stuff here

    echo ..
  }

}

Now when I call one of these methods:

$atheme = new Atheme;

$atheme->header();

I want the header () method from the child class to automatically call the parent () method of the parent class without a special call to parent :: header () in the child class.

Is it possible?

+5
source share
1 answer

If you have only one level of inheritance, you can do this by doing something like:

abstract class theme {
    final public function header() {
        // do parent class stuff
        $this->_header();
    }

    abstract protected function _header();

    // ditto for footer
}

class Atheme extends theme {
    protected function _header() {
        // do child class stuff
    }
}

If the inheritance chain is arbitrarily long, you need to use a callback chain.

+4
source

All Articles