Skip to content

Lesson 7 of 9 · 19m

Unlearning PHP 5: The Habits That Still Create Production Incidents

The legacy patterns behind modern PHP bugs — error suppression, stringly typed state, superglobal soup, and arrays pretending to be objects.

A codebase can run on PHP 8.3 and still be mentally written for PHP 5.2. You see it in @-suppressed filesystem calls, associative arrays passed through seven layers, boolean flags encoded in strings, and functions that return false for failure, null for missing, and an object for success. Updating the interpreter does not update the design habits. Those habits are what migration work must remove.

The failure-shaped return value

Legacy PHP loved overloaded returns:

function findUser(int $id): array|false|null

Callers then guessed what false meant. Was the user absent? Did the database fail? Was input invalid? Modern PHP separates these concerns: return nullable when absence is normal, throw when the operation failed, and use result objects when domain failure is expected input rather than an exception.

function user(int $id): ?User
{
    // null means no such user; database failure throws
}

Superglobals are input, not dependency injection

Reading $_POST deep inside a domain service makes the code impossible to test without mutating global state. The boundary — controller, command handler, worker — should parse and validate input, then pass typed values inward. The same rule applies to $_SESSION, $_COOKIE, and $_SERVER.

final class CreateOrder
{
    public function handle(CustomerId $customer, Money $total): OrderId {}
}

Now the unit test constructs values directly, and the HTTP layer remains replaceable.

Arrays are data structures, not domain models

An array is appropriate for a list of rows or decoded JSON at the boundary. It is not appropriate as the canonical representation of an invoice through the domain layer. Every if (!isset($row['total_cents'])) is an admission that the object model never formed.

Error suppression hides operational reality

The @ operator does not fix failure; it merely removes your ability to observe it. Modern APIs mostly throw, but filesystem, stream, and some extension functions still emit warnings. Convert boundary failures explicitly:

$contents = file_get_contents($path);

if ($contents === false) {
    throw new RuntimeException("Could not read {$path}");
}

Better still, use filesystem abstractions that throw meaningful exceptions and preserve the underlying error.

The mysql era never really ended

Even when old mysql_* calls are gone, concatenated SQL persists in PDO-era code. Interpolating identifiers or values because “it is internal” is how internal tools become breach reports. Prepared statements are the baseline; query builders are acceptable only if they never concatenate user-controlled fragments.

Field notes

  • Search for @, extract(, compact( abuse, eval, global, and dynamic variable names during audits. They are reliable legacy fault lines.
  • A function returning more than two meaningful outcomes needs a result object or exceptions, not another sentinel.
  • Legacy code often uses == “because it worked before.” Strict comparison is behavior-changing; migrate it with tests, not grep confidence.
  • isset() returns false for null values as well as missing keys. Use array_key_exists when presence itself matters.
  • Do not confuse cosmetic modernization — arrow functions and short arrays — with architectural modernization. The latter is where risk lives.

Was this lesson helpful?

Sign in to rate