User Tools

Site Tools


essentials:types_and_signatures

1. Types & Signatures — The Shape of Modern PHP

Why this matters

Modern PHP is typed,
but not statically like Rust or Go,
and not loosely dynamic like early PHP.
— it lives between the two:

Modern PHP is a language where
types clarify intent, reduce mistakes, and support architectural thinking.

AI often generates code
that mixes eras
:

  • missing types
    leaving intent unclear and errors undiscovered
  • overly loose types
    allowing values that don’t belong
  • nullable types everywhere
    signaling uncertainty where none should exist
  • mismatched return signatures
    breaking expectations across the codebase
  • inconsistent parameter order
    making functions harder to read and reason about

This page gives us the mental model to read function signatures with clarity
— and to recognize when the AI has slipped out of the modern style.


1. Types communicate intent

A modern PHP function begins with a clear contract like:

function loadUser(int $id): User { ... }

This tells us the following:

  • what the function expects
  • what it guarantees
  • what cannot happen

Here, types are not decoration — they are architecture.


2. Scalar types are strict

Modern PHP now enforces:

  • int, float, string, bool

This eliminates a whole category of silent coercions that used to cause bugs.

So, if AI generates a function without types, it’s a sign of outdated patterns.


3. Nullable types express optionality

function find(int $id): ?User { ... }

This means that:

  • the function may return a User
  • or it may return null
  • and callers must handle both

Nullable types are explicit — they force clarity.


4. Union types express alternatives

function parse(string $input): int|float { ... }

Union types are powerful, but should be used sparingly.
So, if AI uses them everywhere, it’s usually a sign of overgeneralization.


5. Mixed is a last resort

function handle(mixed $value): void { ... }

mixed is not a type — it’s an admission of uncertainty.
We should use it only when the domain truly requires it.


6. Return types matter as much as parameters

A missing return type is a missing promise.

function create(): User { ... }

This is modern.
And clear.
This is what we want AI to generate.


7. The signature is the architecture

A function signature is a small contract:

  • parameters
    what the function needs in order to do its work
  • types
    the shape and meaning of each parameter
  • nullability
    whether absence is allowed — and what that implies
  • return type
    the kind of value the function promises to give back
  • exceptions (implicit)
    the ways the function may fail, even if not declared

Once we internalize these concepts, we can evaluate AI‑generated functions at a glance — seeing immediately whether the signature matches the intent, the domain, and the architecture.


Summary

Types, signatures, and structure are how we engineer context
— and how we guide the AI toward modern PHP.


essentials/types_and_signatures.txt · Last modified: by editor