Home >Backend Development >PHP Tutorial >Deprecated Practice: Assigning the Return Value of \'new\' by Reference?
Assigning the return value of new by reference, as seen in the code:
<code class="php">$obj_md = new MDB2();</code>
has been marked as deprecated. This practice may lead to an error message: "Assigning the return value of new by reference is deprecated."
In PHP 5, the idiom of assigning by reference is deprecated. The warning can be removed by simply omitting the ampersand from the code:
<code class="php">$obj_md = new MDB2();</code>
In PHP 4, the idiom of assigning by reference was used to extend classes. For instance, the following code would extend the MDB2 class:
<code class="php">$obj_md =& new MDB2();</code>
In the provided code sample, it is possible that an ampersand is missing, which would result in the aforementioned error. In this case, the code should be:
<code class="php">$obj_md =& new MDB2();</code>
However, this idiom is deprecated and should be avoided.
The above is the detailed content of Deprecated Practice: Assigning the Return Value of \'new\' by Reference?. For more information, please follow other related articles on the PHP Chinese website!