Character in PHP I've never encountered before

I probably should have, but I've never seen this before. Get into it by looking at the Smarty Plugin documentation.

$smarty =& new Smarty;

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

The same applies to this function signature:

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

Why a symbol?

+3
source share
8 answers

Actually, this code is written for compatibility with PHP 4. Ampersand is useless in PHP 5 (as Tim said - starting with PHP 5, all objects are passed by reference).

PHP 4 . , :

$ref_on_my_object =& new MyObject();

- PHP 5, :

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

"" ... PHP ( ), . , " " , :

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

...

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

:

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

:

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

& . , connect() $smarty, .

, =& .

+4

, . PHP, . , .

+3

, - .

PHP manual

+2

& PHP. . " Smarty".

+1

, .

0

. , , , .

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

print_r($var);

this should output

array( test , test2 );

, [ ], , /. , , , C/++ .

0

Source: https://habr.com/ru/post/1724932/


All Articles