Call_user_func by methods

Is there a difference between:

$callback = array(&$this, 'method'); $callback[0]->$callback[1]($args); 

and

 call_user_func(array(&$this, 'method'), $args); 

?

+4
source share
3 answers

I have not seen the first use case, but it seems valid and should be faster than call_user_func() , since you have no overhead when calling another function.

UPDATE:

In addition, you cannot do this with call_user_func() if you enabled the error level E_STRICT:

 // ... public function &example($foo) { $this->bar = 'foo'; return $this->bar; } // ... $dummy = &$callback[0]->$callback[1]($args); 

In this case, call_user_func() will output something like this:

Strict PHP standards: only variables should be assigned by reference in php shell code in line X

+1
source

No, there is no difference between calling a method / function of a variable and using call_user_func . I did not encounter a situation when I needed the latter. By the way, you do not need to pass $this by reference; all objects are automatically transferred by reference.

+2
source

It makes no difference, but I prefer the latter for readability. The first one is less clear and takes two lines ...

+2
source

Source: https://habr.com/ru/post/1390270/


All Articles