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
- What are the most important features introduced in PHP 8.0?
- What happens to an HTTP request in a PHP-FPM application, and what does 'shared nothing' mean?
- How should passwords be stored in PHP?
- What are named arguments and what are their benefits and risks?
- What is constructor property promotion?
- How does `match` differ from `switch`?
- What are union types in PHP 8 and how do you use them?
- What are enums in PHP 8.1? Explain pure vs backed enums.