There is such code:

class main { const MYCONST = "Hello world"; public function printout() { echo $this::MYCONST, "\n"; echo self::MYCONST; } } $obj = new main; $obj->printout(); 

What is the difference between calls

 $this::MYCONST; self::MYCONST; 
  • And you know what $this from self differs? - Naumov
  • More food for thought : static::MYCONST ;) - xEdelweiss
  • $ this - for objects inside the class self - for use in a static context inside the class - MaximPro
  • I know about static and later static binding too, I'm not interested in static - MaximPro

1 answer 1

You can call self::MYCONST from any class method, from normal and from static. You can access $this::MYCONST only from the object, i.e. in a static class method, such a call will cause an error

 class main { const MYCONST = "Hello world"; public static function printout() { echo $this::MYCONST, "\n"; } } echo main::printout(); // Fatal error: Class name must be a valid object or a string i 
  • Well, if this is the only difference, then it is a pity, because there is nothing special - MaximPro
  • @MaximPro features with self begin with inheritance, at the class level, yes, nothing special. - cheops
  • Yes, I know in such cases use static =) - MaximPro
  • tested ... out $ this = static? So? - MaximPro