tags:

views:

384

answers:

8

I probably should have, but I've never seen this before. Ran into it when looking over the documenation of a Smarty Plugin.

$smarty =& new Smarty;

The =& sign in particular. If you enter it in Google, it gets ignored, just like any other search engine. What is this used for?

Same goes for this function signature:

function connect(&$smarty, $reset = false)

Why the & symbol?

A: 

The ampersand will assign a reference to the variable, rather than the value of the object.

Tim
+1  A: 

& is PHP's reference operator. It's used to return a reference to the object. In this case "new Smarty".

Pablo Santa Cruz
+13  A: 

It is used for passing values by reference rather than by value which is default in php.

Sarfraz
Also used for returning references
Peter Lindqvist
+4  A: 

& passes an argument by reference. In this fashion, connect() can manipulate the $smarty object so that the calling function can retrieve the modified object.

Similarly, =& sets a variable by reference.

Paul Lammertsma
+3  A: 

As Tim said its a reference to a variable. But if you're using a recent version of PHP then all class object are passed by reference anyway. You would still need this if you were passing about arrays, or other builtin types though.

Michael Anderson
+2  A: 

The first example is returning reference, the second is passing reference.

You can read all about it in the PHP manual

Peter Lindqvist
+11  A: 

Actually, this code is written to be compatible with PHP 4. The ampersand is useless in PHP 5 (as Tim said - since PHP 5, all objects are passed by reference).

With PHP 4, all variables were passed by value. If you wanted to pass it by reference, you had to declare a reference assignment :

$ref_on_my_object =& new MyObject();

This code is still accepted with PHP 5 default configuration, but it's better to write :

$ref_on_my_object = new MyObject(); // Reference assignment is implicit

For your second problem, the issue is "almost" the same... Because PHP lets you declare function arguments (resp. types), and you can't do it for return values. An accepted, but "not so good" practice is to avoid reference declaration within the function's declaration :

function foo($my_arg) {    
    // Some processing
}

and to call with a reference...

$my_var;
$result = foo( &$my_var );
// $my_var may have changed because you sent the reference to the function

The ideal declaration would be more like :

function foo( & $my_input_arg ) {    
    // Some processing
}

then, the call looses the ampersand :

$my_var;
$result = foo( $my_var );
// $my_var may have changed because you sent the reference to the function
Arno
Clearly the best answer.
MiseryIndex
A: 
krob
Actually, i don't think you're passing the memory address...I think, you're dealing with a reference to the zval which holds the data...
Arno
I understand. I was merely trying to imply that the language will use a reference similar in conjunction with how C/C++ do internally. Not that it actually exposes the memory address to the user since this is protected information the user should not know about.
krob