What is the difference between self and __CLASS__? [duplicate]

0

Studying the Singleton design pattern, I was testing:

class Test {
    public static $var = "XYZ";
    static function class() {
        return __CLASS__;
    }
    static function self() {
        return self::$var;
    }
    static function compare() {
        return self === __CLASS__;
    }
}

echo Test::$var; //XYZ
echo Test::class()::$var; //XYZ
echo Test::self(); //XYZ
var_dump(Test::compare()); //bool(false)

I noticed that self is used to access a variable or function within the class, and __CLASS__ returns the reference of the class itself

Am I right? Is that the only difference between the two? Are there cases where you both use one or the other? Why do I do this does not work?

class Test {
    public static $var = "XYZ";
    static function class() {
        return __CLASS__::$var;
    }
    static function self() {
        return self;
    }
}

echo Test::class(); //syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting ';'
echo Test::self()::$var; //Uncaught Error: Class 'self' not found
    
asked by anonymous 03.09.2018 / 22:10

0 answers