PHP - extend method like extending a class
Asked Answered
S

1

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.

Sutton answered 18/6, 2013 at 3:38 Comment(0)
V
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.


Venu answered 18/6, 2013 at 3:55 Comment(10)
This is great, I never thought to call a child method from parent!Sutton
Yes, that's a design I often use. Pretty handy in certain circumstances.Venu
@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 !Venu
Do you know that this is Template method pattern?Lavalava
Hehe ! Honestly I never put a name on this. Just saw it and used it... Thanks you ;-)Venu
No, I didn't know this name. And this is the first time I find useful for parent calling subclass methodSutton
@Sutton 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 ?Venu
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.Sutton
@GauthierBoaglio How can I check in p.f. walk(); if the function walkMyWay(); is defined?Yuan
@Yuan What you are probably looking for is pure virtual behavior - google about that term - (which does not exist in PHP, but can be approached)... https://mcmap.net/q/740372/-what-39-s-the-equivalent-of-virtual-functions-of-c-in-php/1715716Venu

© 2022 - 2024 — McMap. All rights reserved.