Symbol in PHP I've never come across before

喜夏-厌秋 提交于 2019-12-11 03:03:09

问题


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?


回答1:


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




回答2:


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



回答3:


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




回答4:


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.




回答5:


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

You can read all about it in the PHP manual




回答6:


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




回答7:


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




回答8:


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.



来源:https://stackoverflow.com/questions/1859133/symbol-in-php-ive-never-come-across-before

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!