Global Function Style vs OOP Evolution
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();
}
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
19
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Amazonbot 8
Google 4
Unknown AI 3
Perplexity 2
ChatGPT 1
Ahrefs 1
Also referenced
How they use it
crawler 15
crawler_json 2
pre-tracking 2
Related categories
⚡
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