KdgDev
KdgDev

Reputation: 14529

Symbol in PHP I've never come across before

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?

Upvotes: 2

Views: 623

Answers (8)

krob
krob

Reputation: 101

One of the primary uses of the ampersand operator is to pass by memory address. This is usually something you do when you want to have a variable changed, but not be returned.

function test_array(&$arr)
{
    $varr[] = "test2";
}
$var = array('test');
test_array($var);

print_r($var);

this should output

array( test , test2 );

The purpose of this is usually when you need to pass the actual copy[memory address] you are working with into another function / object. Typically it was used in the past to alleviate a lack of memory and speed up performance, it's a feature from C / C++ and a few other low level languages.

Upvotes: 0

Arno
Arno

Reputation: 723

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

Upvotes: 15

Peter Lindqvist
Peter Lindqvist

Reputation: 10200

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

You can read all about it in the PHP manual

Upvotes: 2

Michael Anderson
Michael Anderson

Reputation: 73490

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.

Upvotes: 3

Paul Lammertsma
Paul Lammertsma

Reputation: 38252

& 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.

Upvotes: 4

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181290

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

Upvotes: 1

Tim Ebenezer
Tim Ebenezer

Reputation: 2724

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

Upvotes: 0

Related Questions