PHP 8 interview question · Most asked

What is late static binding?

Summary

Late static binding means static:: resolves to the class that was actually called at runtime, whereas self:: always resolves to the class where the method is written.

Longer Answer

It enables inheritable static factories and active-record patterns: User::create() defined in Model can instantiate User via new static(). parent:: still refers to the direct parent.

Code Example

<?php
class Model {
    public static function table(): string { return static::TABLE; }
}
class User extends Model { const TABLE = 'users'; }

echo User::table(); // users

Key Points

  • self:: = defining class (compile time)
  • static:: = called class (runtime)
  • Use new static() for inheritable factories

More most-asked PHP 8 questions

← All PHP 8 questions