Home > Backend Development > PHP Tutorial > Why Is Assigning New Return Values by Reference Deprecated?

Why Is Assigning New Return Values by Reference Deprecated?

Patricia Arquette
Release: 2024-10-24 04:28:30
Original
356 people have browsed it

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();
Copy after login

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();
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template