tags:

views:

110

answers:

3
call_user_func(array(&$this, 'method_name'), $args);

I know it mean pass-by-reference when defining functions,but is it when calling a function?

+1  A: 

Its a pass-by-reference.

Visage
Am I right in thinking it no longer applies from PHP 5 onwards?
Ian Oxley
I know it mean pass-by-reference when defining functions,but is it when calling a function?
Shore
@Ian you are correct, it's been deprecated.
alex
@Ian: Partly. Objects are always passed by reference, whereas anything else is copied. It does not make any sense with $this, but might be useful for large array, for example.
soulmerge
+1  A: 

It's pass by reference. Instead of creating a copy to go into the function, it will send the actual variable..

$string = 'test';

hello($string);

function hello(&$s) {
    $s .= ' and some more test';
}

// IIRC, the output will be 'test and some more test'

Update

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

I think this code will call a method name within that class, and send $args as parameters. The pass by reference I assume is that it won't attempt to copy the $this, (I'm not sure if it even can). $this would be the reference to the class.

alex
sorry,I didn't clarify myself,just updated.
Shore
Made an update
alex
So the conclusion is:it's been deprecated,this above is the same as call_user_func(array($this, 'method_name'), $args);?
Shore
@Shore I believe so, but don't take my word on it. I may be incorrect. Does removing the ampersand cause any problems or performance problems?
alex
Not yet,so far so good.
Shore
Okay... well hopefully someone smarter than me comes along and gives some clarification!
alex
A: 

From the Passing By Reference docs page:

You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows:

<?php
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
// $a is 6 here
?>

...In recent versions of PHP you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);

karim79