← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Assigning new by Reference

php PHP 8.1+ Beginner

Also Known As

=&new assign by reference new by reference

TL;DR

Writing $obj = &new ClassName() — assigning a new object by reference is redundant in PHP 5+ where objects are already passed by handle, not value.

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

Without &, new creates a copy of the object — PHP 5+ objects are passed by handle (object identifier), not copied on assignment; & is redundant and deprecated.

Why It Matters

Code with $obj = &new triggers E_DEPRECATED in PHP 7 — it is dead weight from PHP 4 that signals the code has not been maintained and may contain other outdated patterns.

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

✗ Vulnerable
// 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
✓ Fixed
// 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

Added 16 Mar 2026
Edited 22 Mar 2026
Views 22
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S
No pings yesterday
Amazonbot 7 Perplexity 4 Unknown AI 2 ChatGPT 2 Google 1 Ahrefs 1
crawler 16 crawler_json 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
PHP 8.1 allows passing new expressions by reference: fn(&$obj = new Foo()) — but prefer returning new objects or using constructor injection over reference parameters in modern PHP
📦 Applies To
PHP 8.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
PHP 8.0 workaround for passing new expression by reference; reference parameters that could be return values instead
Auto-detectable: ✓ Yes rector phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Line

✓ schema.org compliant