{
    "slug": "php4_global_functions",
    "term": "Global Function Style vs OOP Evolution",
    "category": "php",
    "difficulty": "beginner",
    "short": "PHP 4's procedural global-function style evolved into PHP 5 OOP — understanding the transition explains why modern PHP has both paradigms and how to migrate legacy procedural code.",
    "long": "PHP 4 was primarily procedural: functions like mysql_query(), ereg(), split() in global scope, state managed through global variables, no namespaces, everything in include files. PHP 5 introduced proper OOP (classes with visibility, interfaces, abstract classes). PHP 7 added scalar types. Modern PHP is primarily OOP with namespaces and Composer, but the global function library remains (date(), array_map(), file_get_contents()). Legacy codebases often mix both — procedural logic with OOP classes. Migration path: wrap procedural code in classes, add namespaces, replace mysql_ with PDO, use Composer autoloading.",
    "aliases": [],
    "tags": [
        "php",
        "history",
        "procedural",
        "oop",
        "migration"
    ],
    "misconception": "PHP functions like array_map() are deprecated — built-in global functions are permanent. The OOP evolution affected application code structure, not the standard library.",
    "why_it_matters": "Understanding the procedural→OOP transition helps identify which patterns in legacy codebases need modernisation and which global functions are still appropriate to use.",
    "common_mistakes": [
        "Wrapping every function in a class unnecessarily — global functions are fine for pure utility.",
        "Not namespacing custom global functions — use namespaces to avoid collisions.",
        "Keeping mysql_ extension calls instead of migrating to PDO."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php4_era",
        "php5_oop_model",
        "namespaces",
        "composer"
    ],
    "prerequisites": [
        "php4_era"
    ],
    "refs": [
        "https://www.php.net/manual/en/history.php.php"
    ],
    "bad_code": "// PHP 4 style — global state, no OOP:\nglobal $db;\n$result = mysql_query('SELECT * FROM users', $db);\nwhile ($row = mysql_fetch_array($result)) {\n    echo $row['name'];\n}",
    "good_code": "// Modern — OOP with PDO:\nclass UserRepository {\n    public function __construct(private PDO $pdo) {}\n\n    public function findAll(): array {\n        return $this->pdo->query('SELECT * FROM users')->fetchAll();\n    }\n}",
    "quick_fix": "Wrap procedural code in namespaced classes. Replace mysql_ with PDO. Use Composer autoloading. Add type hints progressively.",
    "severity": "medium",
    "effort": "high",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php4_global_functions",
        "html_url": "https://codeclaritylab.com/glossary/php4_global_functions",
        "json_url": "https://codeclaritylab.com/glossary/php4_global_functions.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": "[Global Function Style vs OOP Evolution](https://codeclaritylab.com/glossary/php4_global_functions) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php4_global_functions"
            }
        }
    }
}