Monday, 13 October 2014

Re: [dcphp-dev] PHP self:: vs static::


Inheritance is where this really shows up.

- static:: refers to the calling class, ie " references the class that was initially called at runtime. "
- self:: refers to the class that actually defined the method

About static:

http://us2.php.net/manual/en/language.oop5.static.php

In particular, see the examples here:

http://us2.php.net/manual/en/language.oop5.late-static-bindings.php

Since :: is the scope resolution operator, you can remember the distinction as "'self' limits the scope to the defining class"

On 10/12/2014 04:32 PM, Ray wrote:
In this code snippet, I get output that says "keykey"    What is the difference between the meanings of static:: and self:: in  this or any other context.  Any man page reference would be gratefully  received!    <?php  error_reporting(E_ALL);    Class Thing  {      const PRIMARY_KEY = 'key';        public function keys()      {          $x = self::PRIMARY_KEY;          $y = static::PRIMARY_KEY;          return $x . $y;      }  }    $thing = new Thing;  var_dump($thing->keys());    Thanks and regards, Ray    

0 comments:

Post a Comment