Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Instantiate class by reference?

I'm converting some old PHP 4.x code for PHP 5.3. I've come across the following, and I'm not sure what it does.

$variable =& new ClassName();

What is the difference between that, and:

$variable = new ClassName();
like image 299
Ryan Parman Avatar asked Jan 22 '23 21:01

Ryan Parman


1 Answers

In Ye Olde Days of PHP4, =& was necessary when constructing objects. In PHP 5, it's not.

=& does reference assignment.

E.G.:

$a = 'a';
$b =& $a;
$b = 'b';
echo $a; // Prints 'b', since a and b have been linked by &=.

In other words, it has its uses, just not when instantiating an object. For that use, it's been depreacted.

like image 115
Tordek Avatar answered Jan 31 '23 14:01

Tordek