Home >Backend Development >PHP Tutorial >Why Is Assigning New Return Values by Reference Deprecated?
Deprecated Assigning of New Return Values by Reference
When attempting to assign an object with the syntax $obj_md = new MDB2();, users may encounter a deprecation error. This error occurs because the practice of assigning the return value of new by reference is outdated in PHP5.
In prior versions of PHP (such as PHP4), the following syntax was common:
$obj_md =& new MDB2();
However, this idiom is now strongly discouraged. The ampersand (&) in the above example denotes a reference to the object. In PHP5, it is generally considered better practice to assign the object directly without using a reference. This can be done by removing the ampersand, as follows:
$obj_md = new MDB2();
It is important to note that while the deprecation warning exists, the resulting code should still function correctly. The only exception to this is if you have explicitly disabled error reporting in your PHP environment.
The above is the detailed content of Why Is Assigning New Return Values by Reference Deprecated?. For more information, please follow other related articles on the PHP Chinese website!