Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use parameters of parent class method with the same method name?

The title is a bit confusing, I know, but it's the best I could do. =P

Hopefully, someone will be able to help.

I'm using CodeIgniter and I have a method in a base class that has multiple parameters:

class MY_Base extends CI_Model {

    function some_function($param1, $param2 ... $param8) {
        // do stuff
    }
}

What I want to do is basically this, in a child class:

class Child extends MY_Base {

    function some_function($param1, $param2 ... $param8) {
        parent::some_function($param1, $param2 ... $param8);
        // call a methods found in this class only
        $this->some_method();
    }

    function some_method() {
        // do more stuff
    }
}

I can't touch the base classes so I have to extend from it. Problem is, There are just too many parameters. And this happens in different methods as well and sometimes I forget one which makes the code fail.

So I was wondering, if there's a way to write it like this:

function some_function(__PARAMETERS__) {
    parent::some_function(__PARAMETERS__)
}

I seem to vaguely recall that this is possible but I can't find it in Google. Probably because I'm searching the wrong keywords.

Any help would be appreciated.

EDIT: And then, of course, I find func_get_args() after posting this question. This seems to do what I want, but I'll leave this question up for better ideas.

like image 771
rgin Avatar asked Dec 07 '22 14:12

rgin


1 Answers

For PHP >=7.0 I use:

parent::{__FUNCTION__}(...func_get_args())
like image 51
Audiophile Avatar answered Dec 10 '22 11:12

Audiophile