Skip to content

Lesson 1 of 9 · 22m

Typed Everything: strict_types, Properties, and Honest Signatures

How PHP's type system actually behaves at runtime, where coercion still lurks, and the type vocabulary you should use instead of arrays-everywhere.

The first production PHP 8 migration I led did not fail because of a missing extension or a removed function. It failed because half the application relied on silent coercion: '12abc' becoming 12, null flowing into string parameters, and methods returning five unrelated shapes depending on the moon phase. Modern PHP gives you the tools to stop negotiating with ambiguity, but you have to turn them on deliberately.

strict_types is caller-side

<?php
declare(strict_types=1);

function charge(int $cents): void {}

charge("1000");   // TypeError when strict_types=1 is in this calling file

This surprises everyone once: strict_types applies to calls made from the file that declares it, not to functions defined there. If your legacy endpoint files omit the declaration, they can still coerce values when calling strictly typed library code. That is why migrations add declare(strict_types=1); file by file, preferably enforced by PHP CS Fixer or Rector, instead of assuming one vendor-level setting covers the world.

Use the vocabulary the runtime gives you

PHP 8.x types are not decorative docblocks. They are runtime contracts:

final class Refund
{
    public function __construct(
        public int $cents,
        public string|Currency $currency,
        public ?string $reason = null,
    ) {}

    public function approve(): static
    {
        return $this;
    }
}

Use union types for genuinely alternative inputs, ?string only when absence is meaningful, and never for methods that always throw or exit. Use mixed when arbitrary input is real — decoding JSON, for example — then narrow it immediately. array is the weakest useful type: prefer value objects or at least array-shape annotations for static analysis.

Typed properties fail loudly

Unlike Java-style fields, typed properties in PHP have an uninitialized state:

final class User
{
    public int $id;
}

$user = new User();
echo $user->id; // Error: Typed property User::$id must not be accessed before initialization

That behavior is a feature. It catches constructors that forgot a branch. It also means nullable is not the same as uninitialized: ?int $id = null means “known to be absent”; uninitialized means “programmer never established this invariant.”

Type contracts are architecture

The point is not to satisfy the engine. The point is that refactoring becomes mechanical. Once constructors, return types, and boundaries tell the truth, PHPStan or Psalm can trace mistakes across the application before production does.

Field notes

  • declare(strict_types=1); must be the first statement in each PHP file; whitespace before <?php can still break headers and sessions.
  • Scalar coercion remains in non-strict callers. Audit boundary files first: controllers, workers, webhook receivers, and CLI commands.
  • Prefer int cents over float money even in modern PHP. Types do not rescue floating-point arithmetic.
  • static as a return type means late-static-binding static, not merely “some subclass”; use self when subclasses must not be accepted.
  • Add types at boundaries before internals. The places outside data enters are where lies are most expensive.

Was this lesson helpful?

Sign in to rate