What is the difference between static :: Property, ClassName :: property, self :: property?

2

I know that parent::propriedade , you select the parent class property, but I am not able to differentiate these other three when I use within the scope of the class. Both work, but what's the difference?

    
asked by anonymous 02.10.2017 / 20:37

1 answer

2

We're always talking about static properties there.

self::propriedade is the way to refer to a static property within the class code. It's like using this , but it's for static members, not just properties. You can use the class name as well.

nomeClasse::propriedade is the use of the same thing, but it is used outside the class code, ie when it is consuming the class.

And static::propriedade is like self but for constants and if there is inheritance in this class it will take the static property of the derived class and not the base class if there is a static property with the same name in the derivative

class A {
    const X = 'A';
    public function x() {
        echo static::X;
    }
}

class B {
    public static $X = 'B';
    public function x() {
        echo self::$X;
    }
    public function x2() {
        echo B::$X;
    }
}

class C extends A {
    const X = 'C';
}

$c = new C();
$c->x();
echo C::X;
echo c::X;
$b = new B();
$b->x();
$b->x2();
echo b::$X;
echo B::$X;

See running on ideone . And no Coding Ground . Also I put GitHub for future reference .

    
03.10.2017 / 02:25