Assigning new by Reference
debt(d2/e1/b1/t5)
Closest to 'default linter catches the common case' (d3), but slightly better (d2) because PHP itself emits E_DEPRECATED at runtime and both rector and phpstan (listed tools) flag this pattern trivially.
Closest to 'one-line patch' (e1) — remove the & character from the assignment, a single-character fix per occurrence as suggested by quick_fix.
Closest to 'minimal commitment' (b1) — this is a localised syntactic artifact that doesn't shape architecture; it's just legacy noise in individual statements.
Closest to 'notable trap' (t5) — the misconception that & is needed to avoid copying objects contradicts PHP 5+ object handle semantics; a documented gotcha that confuses developers coming from value-semantics languages or old PHP 4 codebases.
Also Known As
TL;DR
Explanation
In PHP 4, objects were copied by value — $obj = new Foo() created a value copy on assignment. PHP 5 changed objects to pass-by-handle (similar to references): assigning an object variable gives a new variable pointing to the same object, not a copy. Therefore $obj = &new Foo() is redundant — the & is a no-op. Worse, in PHP 7+ it generates an E_DEPRECATED warning. The pattern is purely a legacy PHP 4 pattern that has no purpose in modern code.
Common Misconception
Why It Matters
Common Mistakes
- $this->property = &new ClassName() — redundant & on object assignment.
- $obj = &new PDO(...) — object is already passed by handle.
- Confusing object references (all objects) with variable references (&$var).
- Not recognising this as a PHP 4 legacy pattern requiring modernisation.
Code Examples
// PHP 4 legacy — redundant & operator:
$db = &new PDO('mysql:host=localhost', 'root', 'pass');
$service = &new UserService($db);
$this->repo = &new UserRepository();
// Generates: E_DEPRECATED in PHP 7
// No effect in PHP 5+ — objects are always by handle
// Modern PHP — no & needed:
$db = new PDO('mysql:host=localhost', 'root', 'pass');
$service = new UserService($db);
$this->repo = new UserRepository();
// If you genuinely need two variables to reference the same assignment:
$a = new Foo();
$b = &$a; // Reference the variable, not the construction