Late static binding

Two keywords decide which class a static call belongs to, and PHP resolves them at different moments. The first two blocks call the same method through a parent and a child and print the difference.

self binds early

The self keyword always points at the class where the method is written. It ignores later children of that class.

A child that overrides a static property still sees the parent value through self. The binding happens at write time, not at call time.

<?php class Base { protected static string $who = "base"; public static function who(): string { return self::$who; } } class Child extends Base { protected static string $who = "child"; } echo Base::who(), PHP_EOL; echo Child::who(), PHP_EOL;

php self.php

base
base

The call through Child still reads the property of Base, because self was fixed when the method was written.

static:: binds late

The static keyword keeps the called class until the call runs. The same method can then answer per child.

Call the method on the child and static resolves to the child. Call it on the parent and it resolves to the parent.

<?php class Base { protected static string $who = "base"; public static function who(): string { return static::$who; } } class Child extends Base { protected static string $who = "child"; } echo Base::who(), PHP_EOL; echo Child::who(), PHP_EOL;

php static.php

base
child

The same method body now answers twice, once with the parent property and once with the child one.

forward_static_call note

The forward_static_call function passes late binding along to another callable. A plain call_user_func would drop that context.

Reach for the forward form inside static helpers that hand work to the next method.

<?php class A { public static function who(): string { return static::class; } public static function viaForward(): string { return forward_static_call([self::class, "who"]); } public static function viaCallUserFunc(): string { return call_user_func([self::class, "who"]); } } class B extends A { } echo B::viaForward(), PHP_EOL; echo B::viaCallUserFunc(), PHP_EOL;

php forward.php

B
A

Both calls start from B and hand the work to who() through self::class. The forward form keeps the called class, the plain helper resets it to the class named in the callable.

Framework pattern

Frameworks use late binding for shared factories. The parent defines make once, and each child gets its own type back.

The static return type promises the child type, and new static builds it. One factory then serves the whole tree.

<?php class Model { public static function make(): static { return new static(); } } class User extends Model { } $user = User::make(); echo get_class($user), PHP_EOL; echo get_class(Model::make()), PHP_EOL;

php model.php

User
Model

One factory method produced both types, and the static return type describes that promise to tools and readers.

Exercises

  1. Add a second child with its own static property and print the result of static:: for both.
  2. Replace static:: with self:: in a factory and describe the change.
  3. Call a static method through forward_static_call() from a child class and print the class name.
  4. Build a family of two classes with one inherited factory and print get_class() of each result.
  5. Explain why a method that returns new self() breaks a fluent child API.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which class does self name inside a static method?

    Answer

    The class where the method is written

    self is resolved when the code is compiled, so it never follows a child class.

  2. Which class does static name inside a static method?

    Answer

    The class used in the actual call

    Late static binding carries the called class, so a child call answers with the child.

  3. What does forward_static_call() keep that call_user_func() drops?

    Answer

    The called class of the current scope

    The forward call passes the late static binding context on, while call_user_func starts a fresh one.

  4. What does the return type static promise in a factory?

    Answer

    A new object of the class that was called

    The type follows the call, so a child call returns an object of the child class.

  5. What does this program print? <?php class Base { protected static string $who = "base"; public static function who(): string { return self::$who; } } class Child extends Base { protected static string $who = "child"; } echo Child::who();

    Answer

    base

    self points at Base, so the property read is the one of the parent.

  6. What does this program print? <?php class Base { protected static string $who = "base"; public static function who(): string { return static::$who; } } class Child extends Base { protected static string $who = "child"; } echo Child::who();

    Answer

    child

    static resolves to the called class Child, and its own property holds child.

  7. What does this program print? <?php class A { public static function who(): string { return static::class; } public static function run(): string { return forward_static_call([self::class, "who"]); } } class B extends A { } echo B::run();

    Answer

    B

    The forward call keeps B as the called class, so static::class inside who() is B.

  8. What does this program print? <?php class A { public static function who(): string { return static::class; } public static function run(): string { return call_user_func([self::class, "who"]); } } class B extends A { } echo B::run();

    Answer

    A

    call_user_func calls the method on the named class A, so the called class inside who() is A.

  9. What does this program print? <?php class Model { public static function make(): static { return new static(); } } class User extends Model { } echo get_class(User::make());

    Answer

    User

    new static builds the class of the call, which is User.

  10. Why is new self() a bad body for an inherited factory?

    Answer

    It creates the parent class even when a child calls it

    self is fixed in the parent, so every child call gets a parent object back.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev