Tuesday, 13 April 2021

PHP self::varaiable vs static::varaiable

 Using static:: performs Late Static Binding and thus it binds the variable value from child class.

class A { // Base Class
    protected static $name = 'ClassA';
    public static function getSelfName() {
        return self::$name;
    }
    public static function getStaticName() {
        return static::$name;
    }
}

class B extends A {
    protected static $name = 'ClassB';
}

echo B::getSelfName(); // ClassA
echo B::getStaticName(); // ClassB

https://stackoverflow.com/questions/11710099/what-is-the-difference-between-selfbar-and-staticbar-in-php

No comments:

Post a Comment