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

Global Function Style vs OOP Evolution

php PHP 4.0+ Beginner

TL;DR

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.

Explanation

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.

Common 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.

Code Examples

✗ Vulnerable
// PHP 4 style — global state, no OOP:
global $db;
$result = mysql_query('SELECT * FROM users', $db);
while ($row = mysql_fetch_array($result)) {
    echo $row['name'];
}
✓ Fixed
// Modern — OOP with PDO:
class UserRepository {
    public function __construct(private PDO $pdo) {}

    public function findAll(): array {
        return $this->pdo->query('SELECT * FROM users')->fetchAll();
    }
}

Added 23 Mar 2026
Views 19
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S
No pings yesterday
Amazonbot 8 Google 4 Unknown AI 3 Perplexity 2 ChatGPT 1 Ahrefs 1
crawler 15 crawler_json 2 pre-tracking 2
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: High
⚡ Quick Fix
Wrap procedural code in namespaced classes. Replace mysql_ with PDO. Use Composer autoloading. Add type hints progressively.
📦 Applies To
PHP 4.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
mysql_query|mysql_fetch|mysql_connect
Auto-detectable: ✓ Yes rector phpcs
🤖 AI Agent
Confidence: High False Positives: Low ✗ Manual fix Fix: High Context: File Tests: Update
CWE-89

✓ schema.org compliant