Interview frequency: …
Short answer
self refers to the class where the code was written at compile time, while static refers to the class from which the method was actually called at runtime.
Explanation
In inheritance, if a parent class has a static method that creates an instance with new self(), a child class calling that method still creates an instance of the parent. Late Static Binding with new static() tells the engine to resolve the class based on the runtime caller; this is the foundation of Laravel's Model::create() pattern.
Code example
class Model {
public static function make() {
return new static(); // Creates an instance of the calling subclass
}
}
class User extends Model {}
User::make(); // Creates a User, not ModelCommon mistakes
- Using self::class in inherited class factories, which always returns the base class name.