QA
Intermediate LevelPHP 8+

What is the difference between self:: and static:: (Late Static Binding) in PHP inheritance?

❓ Interview Prompt: Compare self:: (compile-time resolution) vs static:: (runtime Late Static Binding) in OOP class hierarchies.

When working with static methods and properties in class inheritance hierarchies:

1. 'self::':

•
Resolved at compile-time.
•
Always references the exact class where the code was written, ignoring child class overrides.

2. 'static::' (Late Static Binding - PHP 5.3+):

•
Resolved at runtime.
•
References the actual class that invoked the method (the child class executing the call). Essential for Active Record ORM patterns ('Model::create()').

Code Implementation Example

php
<?php
class ParentModel {
    public static string $table = 'parents';

    public static function getTableSelf(): string {
        return self::$table; // Compile-time: Always resolves to ParentModel
    }

    public static function getTableStatic(): string {
        return static::$table; // Late Static Binding: Resolves to calling child class!
    }
}

class User extends ParentModel {
    public static string $table = 'users';
}

echo User::getTableSelf();   // Outputs: parents (self:: ignores child override)
echo User::getTableStatic(); // Outputs: users   (static:: respects child override!)
💡Comparing self:: compile-time resolution with static:: Late Static Binding.
🎯 Interview Key Takeaway

Use static:: (Late Static Binding) instead of self:: to resolve static methods and properties to the calling child class at runtime.

#Late Static Binding#static::#self::#OOP#Intermediate
Back to all PHP 8+ Questions→

More PHP 8+ Interview Questions

Beginner LevelPHP

What is PHP and how does its shared-nothing request lifecycle work?

Beginner LevelPHP

What is the difference between echo and print in PHP?

Beginner LevelPHP

What is the difference between isset(), empty(), and is_null() in PHP?