@daniloparrajr

PHP Late Static Binding

  • 7/14/2024
  • 1 min read

The Problem

On static methods, we use “self” to access static properties and methods. The problem is that the “self” keyword determines the calling class on compile time. It will always resolve to the class in which it is defined. So in the code below, Electric_Car::getBrand() will always return Toyota instead of Tesla.

class Car {
protected static string $brand = 'Toyota';
public static function getBrand(): string
{
return self::$brand;
}
}
class Electric_Car extends Car {
protected static string $brand = 'Tesla';
}
echo Car::getBrand() . PHP_EOL; // Toyota
echo Electric_Car::getBrand() . PHP_EOL; // Toyota

The Solution: static keyword

PHP introduce the static keyword on methods to get the calling class on run time.

class Car {
protected static string $brand = 'Toyota';
public static function getBrand(): string
{
return static::$brand;
}
}
class Electric_Car extends Car {
protected static string $brand = 'Tesla';
}
echo Car::getBrand() . PHP_EOL; // Toyota
echo Electric_Car::getBrand() . PHP_EOL; // Tesla

Related Articles

PHP Traits

Learn how to reduce code duplication and improve code re-use using PHP Traits.

  • 7/16/2024
  • 3 min read