Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using call_user_function to access parent method in PHP

Is there any way, in PHP, to call methods from a parent class using the arbitrary-argument call_user_func_array? Essentially, I want to write a little bit of boilerplate code that, while slightly less optimal, will let me invoke the parent to a method arbitrarily like this:

function childFunction($arg1, $arg2, $arg3 = null) {
    // I do other things to override the parent here...

    $args = func_get_args();
    call_user_func_array(array(parent, __FUNCTION__), $args); // how can I do this?
}

Is this an odd hack? Yeah. I will be using this boilerplate in many places, though, where there's likely to be error in transcribing the method args properly, so the tradeoff is for fewer bugs overall.

like image 656
Joe Mastey Avatar asked Jun 22 '10 18:06

Joe Mastey


1 Answers

Try either one of

call_user_func_array(array($this, 'parent::' . __FUNCTION__), $args);

or

call_user_func_array(array('parent', __FUNCTION__), $args);

... depending on your PHP version. Older ones tend to crash slightly, careful :)

like image 99
Denis 'Alpheus' Cahuk Avatar answered Nov 09 '22 23:11

Denis 'Alpheus' Cahuk