📚 Encyclopedia

Web Development · updated 2026-10-13

PHP late static binding and forward_static_call()

phplate-static-bindingstaticoopcallables

In PHP, self:: refers to the class where the code is written, while static:: refers to the called class (late static binding). forward_static_call() calls a callable while keeping the current called class, where call_user_func() would reset it. These facts were measured on PHP 8.3.29 for the interactive tutorial at justinahampton.com/fun/experiments/forward-static-call/, where every output shown is captured from real PHP rather than typed in.

The rules (PHP 8.3)

Compared with the written forms

When forward_static_call is actually the right tool

  1. Skipping a generation: Admin extends User extends Model, and Admin wants Model's all() while User overrides it. parent::all() runs User's override; Model::all() resets static to Model; forward_static_call([Model::class, 'all']) runs Model's method with static still Admin.
  2. The callable is a value (a string or [Class::class, 'method'] chosen at runtime): parent::$step() cannot express an arbitrary target class, $step() resets the called class, forward_static_call($step) keeps it.
  3. Where parent:: wins: by-reference arguments and named arguments both work with parent::method() and not with forward_static_call. The choice is about the destination: same chain, one level up, use parent::; anywhere else in the chain while keeping static, use forward_static_call.

Style note

Show modern syntax even when quoting old docs (Justin's preference): [A::class, 'test'] rather than array('A', 'test'), typed parameters, string ...$args, : void, no closing ?>.

Capturing real output for docs

The desktop has no PHP, so a capture script tars every snippet to the server, runs each with the server's PHP CLI in a temporary directory it deletes, and regenerates a truth.js the page reads. Rerun after editing any snippet; a test compares a small JS model of the rules with the captured outputs.

See also

Sources: justinahampton memory (experiments-forward-static-call), tldr posts #1743, #1758, measured on PHP 8.3.29