{
    "slug": "reference_new",
    "term": "Assigning new by Reference",
    "category": "php",
    "difficulty": "beginner",
    "short": "Writing $obj = &new ClassName() — assigning a new object by reference is redundant in PHP 5+ where objects are already passed by handle, not value.",
    "long": "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.",
    "aliases": [
        "=&new",
        "assign by reference",
        "new by reference"
    ],
    "tags": [
        "php",
        "legacy",
        "style",
        "deprecated"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "removed_function",
        "legacy_date_functions",
        "deprecated_functions"
    ],
    "prerequisites": [
        "php_data_types",
        "variadic_functions",
        "php_closures_advanced"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.oop5.references.php"
    ],
    "bad_code": "// PHP 4 legacy — redundant & operator:\n$db      = &new PDO('mysql:host=localhost', 'root', 'pass');\n$service = &new UserService($db);\n$this->repo = &new UserRepository();\n\n// Generates: E_DEPRECATED in PHP 7\n// No effect in PHP 5+ — objects are always by handle",
    "good_code": "// Modern PHP — no & needed:\n$db      = new PDO('mysql:host=localhost', 'root', 'pass');\n$service = new UserService($db);\n$this->repo = new UserRepository();\n\n// If you genuinely need two variables to reference the same assignment:\n$a = new Foo();\n$b = &$a; // Reference the variable, not the construction",
    "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",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/reference_new",
        "html_url": "https://codeclaritylab.com/glossary/reference_new",
        "json_url": "https://codeclaritylab.com/glossary/reference_new.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Assigning new by Reference](https://codeclaritylab.com/glossary/reference_new) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/reference_new"
            }
        }
    }
}