Home  >  Article  >  Backend Development  >  Deprecated Practice: Assigning the Return Value of \'new\' by Reference?

Deprecated Practice: Assigning the Return Value of \'new\' by Reference?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-23 22:04:30700browse

Deprecated Practice: Assigning the Return Value of 'new' by Reference?

Assigning the Return Value of 'new' by Reference: A Deprecated Practice

The Problem

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."

The Solution

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>

The Deprecated Idiom

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 =&amp; new MDB2();</code>

Ampersand Omission

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 =&amp; 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn