2

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?

2
  • note that in the first example, you only need to use this in PHP4.
    – nickf
    Dec 7, 2009 at 11:24
  • 3
    Words work with Google, such as "equals ampersand php".
    – GZipp
    Dec 7, 2009 at 15:02

8 Answers 8

15

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
0
12

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

1
  • Also used for returning references Dec 7, 2009 at 11:13
4

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

3

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.

2

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

You can read all about it in the PHP manual

1

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

0

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

0

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.

2
  • 1
    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
    Dec 7, 2009 at 15:15
  • 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
    Dec 8, 2009 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.