George
George

Reputation: 479

Can I call inherited parent method without a name in PHP?

Is there a way to call an inherited method, without specifying it's function name?

Something like:

class Child extends Parent {
    function some_function(){
        // magically inherit without naming the parent function
        // it will call parent::some_function()
        parent::inherit();

        // other code
    }

    function another_function(){
        // it will call parent::another_function()
        $result = parent::inherit();

        // other code
        return $result;
    }
}

I could think of a hack to do this using debug_backtrace(), get the last function where inherit() was called and access it's parent with the same function name. I was wondering if there's a nicer way instead of using debug functions which are clearly not meant for this.

Upvotes: 2

Views: 445

Answers (3)

Mark Baker
Mark Baker

Reputation: 212412

Dirty, but:

class Adult {
    function mummy(){
        return 'Walk like an Egyptian';
    }

    function daddy(){
        return 'Luke, I am your father';
    }
}


class Child extends Adult {
    function mummy(){
        echo 'Mummy says: ';
        $me = explode('::',__METHOD__)[1];
        echo parent::$me();
    }

    function daddy(){
        echo 'Daddy says: ';
        $me = explode('::',__METHOD__)[1];
        echo parent::$me();
    }
}

$o = new Child();
$o->mummy();
$o->daddy();

EDIT

Actually giving you a parent method called inherit();

class Adult {
    private function mummy(){
        return 'Walk like an Egyptian';
    }

    private function daddy(){
        return 'Luke, I am your father';
    }

    protected function inherit($method) {
        $beneficiary = explode('::', $method)[1];
        return $this->$beneficiary();
    }
}


class Child extends Adult {
    public function mummy() {
        echo 'Mummy says: ', 
            parent::inherit(__METHOD__), 
            PHP_EOL;
    }

    public function daddy() {
        echo 'Daddy says: ', 
            parent::inherit(__METHOD__), 
            PHP_EOL;
    }
}

$o = new Child();
$o->mummy();
$o->daddy();

Upvotes: 1

CBusBus
CBusBus

Reputation: 2359

Dynamically calling functions:

static::$functionName();

In your case:

$func = __FUNCTION__;
parent::$func();

Note: the function name must be a string, if it's the actual function (not really relevant in this context) then it first needs to be converted to its string name first.


Other stuff that your question will probably lead you towards in the long run.

Check out late static binding it's what you're looking for.

Example taken from the linked page.

class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        static::who(); // Here comes Late Static Bindings
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();

Upvotes: 0

Gordon
Gordon

Reputation: 316969

You can use the magic __FUNCTION__ constant.

class A 
{
    function some_function()
    {
        echo 'called ' . __METHOD__;
    }
}

class B extends A 
{
    function some_function()
    {
        call_user_func(array('parent', __FUNCTION__));           
    }
}

 $b = new B;
 $b->some_function(); // prints "called A::some_function"

Instead of

call_user_func(array('parent', __FUNCTION__));  

you can also do

parent::{__FUNCTION__}();

Upvotes: 6

Related Questions