Assigning the return value of new by reference is deprecated

Assigning the return value of new by reference is deprecated

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Assigning the return value of new by reference is deprecated

You encounter the error "Assigning the return value of new by reference is deprecated" when attempting to store a freshly instantiated object into a variable using the & operator. The specific code pattern triggering this issue looks like this:

$obj_md = &new MDB2();

At first glance, this syntax appears intentional; you might believe you are explicitly creating a reference to the new instance so that multiple variables point to the same object memory location. However, in modern PHP versions (5.4 and later), this specific pattern of assigning the result of new directly by reference is deprecated. The engine issues a warning because this usage rarely achieves what developers expect without introducing subtle bugs.

To understand why this happens, you must look at how references work in PHP. When you define a variable with &, you are telling the interpreter to treat that variable as an alias for another existing variable or object. If you write $x = &new Foo(), you are attempting to create a reference before the variable on the left-hand side is fully initialized.

In older PHP versions, this was allowed but often resulted in the reference pointing to a temporary value or behaving unexpectedly depending on the context. Modern PHP has tightened these rules to prevent ambiguity. The deprecation warning appears because the language specification no longer supports taking a reference of a newly constructed object at that exact moment. The compiler expects the left-hand side variable to be a stable, existing entity before it can bind a reference to it.

How to Fix It Correctly

The solution is straightforward and requires removing the ampersand from the assignment operator. You should instantiate the object first, then assign it to your variable:

$obj_md = new MDB2();
// If you genuinely need a reference later, you can create one then:
$another_ref = &$obj_md; 

By separating the instantiation from the reference assignment, you ensure that $obj_md holds a concrete object instance. Once that exists, creating a reference to it via $another_ref = &$obj_md works as intended without triggering deprecation warnings.

It is important to note that in many scenarios, removing the & entirely is the preferred approach unless you have a specific architectural reason to maintain multiple aliases to the same object. Modern PHP handles object identity efficiently, and explicit references are often unnecessary overhead. If your codebase relies on this pattern for legacy compatibility with very old PHP versions (pre-5.4), the deprecation warning might not appear yet, but relying on it is a technical debt that should be cleared immediately to avoid future breakage when upgrading environments.

Why Simply Hiding the Warning Isn't Enough

The source material mentions attempts to resolve this by lowering the error reporting policy in php.ini, specifically adjusting error_reporting or disabling E_DEPRECATED. While suppressing the warning via configuration is a common quick fix, it does not address the underlying code quality. Suppressing deprecation warnings leaves your application vulnerable to silent failures if you upgrade PHP to a version where this behavior becomes fatal.

Disabling these warnings also obscures other potential issues in your stack. A robust development environment should surface deprecations so that refactoring can occur proactively. If you are maintaining an older codebase using MDB2, the fix is purely syntactic; there is no need for complex workarounds or polyfills. Updating the instantiation logic to follow modern standards ensures your application remains compatible across current PHP versions and aligns with best practices for memory management.

If you are building a new project or migrating existing logic, replacing &new ClassName() with standard instantiation is the only viable path forward. This change eliminates the deprecation warning entirely while adhering to the language's design philosophy regarding variable scoping and object identity.

Our network