This is about the Refência.
According to the PHP
References, in PHP, means accessing the same variable content through multiple names
Take an example:
$a = 1;
$b = $a;
$a = 2;
var_dump($a, $b); // a => 2, b => 1
Now an example with the reference operator:
$a = 1
$b =& $a;
$a = 2;
var_dump($a, $b)/ // a= > 2, b => 2
That is to say, $b
points to the value of $a
. It does not have an independent value, but it points to the location of memory where the value of $a
was saved.
In the case of functions, the operator &
can be used in two ways:
I will give an example of passing by reference in the functions:
$meuValor = [1, 2, 3];
function my_function(&$var, $add){
$var[] = $add;
}
my_function($meuValor, 6);
print_($meuValor); // [1, 2, 3, 6];
See that the value of $meuValor
was changed, without the return
within the function and without the re-allocation of the function by the return of my_function
.
This is because $var
is a reference of the variable passed by parameter; that is, the variable $meuValor
.
The use in the Operator new
was discouraged, and, if I’m not mistaken, creates an error E_STRICT
while trying to make.
This occurred in versions prior to PHP 5.
In new versions of PHP, objects are passed by default reference.
But what is the meaning of the use together with the operator
new
?(on the link posted has the use) the same?– Ricardo
I’m trying to find out, this is very strange for me. Even p/PHP :)
– Maniero
That was used in php4 if I’m not mistaken.
– rray
PHP 4 Object Orientation was a pure gambiarra :\
– Wallace Maxters