Is there any difference between the following?
$myVar=&new MyClass();
$myVar=new MyClass();
I am coding in PHP
If using PHP5, there is no difference.
If using PHP4, the former passes a reference to the new object.
This is because "new" does not return a reference by default, instead it returns a copy.
Source.
Give this a read, too.
Using PHP5? Then you should use the following:
$myVar=new MyClass();
This is the accepted way to initialize a class.
Using the following when E_STRICT
is enabled:
$myVar=&new MyClass();
will cause the PHP runtimes to raise the warning Deprecated: Assigning the return value of new by reference is deprecated
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With