18

I have 2 class:

class animal{
    public function walk(){
        walk;
    }
}

class human extends animal{
    public function walk(){
        with2legs;
    }
}

This way, if i call human->walk(), it only runs with2legs;

But I want the run the parent's walk; too.

I know I can modify it this way:

class human extends animal{
    public function walk(){
        parent::walk();
        with2legs;
    }
}

But the problem is, I have many subclasses and I don't want to put parent::walk(); into every child walk(). Is there a way I can extend a method like I extend a class? Without overriding but really extending the method. Or is there better alternatives?

Thanks.

Tony
  • 425
  • 1
  • 5
  • 12

1 Answers1

32

I would use "hook" and abstraction concepts :

class animal{

    // Function that has to be implemented in each child
    abstract public function walkMyWay();

    public function walk(){
        walk_base;
        $this->walkMyWay();
    }
}

class human extends animal{
    // Just implement the specific part for human
    public function walkMyWay(){
        with2legs;
    }
}

class pig extends animal{
    // Just implement the specific part for pig
    public function walkMyWay(){
        with4legs;
    }
}

This way I just have to call :

// Calls parent::walk() which calls both 'parent::walk_base' and human::walkMyWay()
$a_human->walk();      
// Calls parent::walk() which calls both 'parent::walk_base' and pig::walkMyWay()
$a_pig->walk();

to make a child walk his way...


See Template method pattern.


nook
  • 1,729
  • 1
  • 12
  • 34
Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85
  • This is great, I never thought to call a child method from parent! – Tony Jun 18 '13 at 04:11
  • Yes, that's a design I often use. Pretty handy in certain circumstances. – Gauthier Boaglio Jun 18 '13 at 04:14
  • 1
    @OneTrickPony I don't know what Tony expects and what is his real use case which pushed him to ask this question, but this is one way of doing. And at worst, he discovered a new possibility ;-). Bed time for me... Cheers ! – Gauthier Boaglio Jun 18 '13 at 04:26
  • 4
    Do you know that this is [Template method pattern](http://en.wikipedia.org/wiki/Template_method_pattern)? – sectus Jun 18 '13 at 04:36
  • Hehe ! Honestly I never put a name on this. Just saw it and used it... Thanks you ;-) – Gauthier Boaglio Jun 18 '13 at 04:44
  • 1
    No, I didn't know this name. And this is the first time I find useful for parent calling subclass method – Tony Jun 18 '13 at 06:35
  • @Tony Well, you unaccepted the answer. Not that I absolutely want to get some more points, but : Does the answer satisfy your needs, or are you looking for some reason for something more ? – Gauthier Boaglio Jun 18 '13 at 12:07
  • I am employing this solution for now but hoping not to stop further ideas. If there are not further answers, I will accept your again, thanks a lot. – Tony Jun 18 '13 at 16:08
  • @GauthierBoaglio How can I check in p.f. walk(); if the function walkMyWay(); is defined? – Ismail May 18 '14 at 19:02
  • @Ismail What you are probably looking for is `pure virtual` behavior - google about that term - (which does not exist in PHP, but can be approached)... http://stackoverflow.com/q/2487705/1715716 – Gauthier Boaglio May 19 '14 at 09:34