3

As of php 5.3, the static keyword resolved the late static binding issue and it can be used like most other OO languages use self. In PHP self still exists.

I know how the two keywords are separate functionally as previously pointed out in this question: Is there any reason to use the self keyword?.

However, my question is: Can anyone give a reason why you would want to use self in code meant to run only with 5.3 or later PHP? Does self have better performance? I've yet to find any compelling use case for using the self keyword anymore. My understanding is it only remains to allow old libraries to continue working in an expected manner.

The key to the question is can anyone suggest a good reason, ANY GOOD REASON, just one, tiny, little, reason that makes sense for a competent PHP developer to say "Gee, here I should really use 'self' instead of 'static'."

Because you might want to shoot yourself in the foot and that's a reason to use it isn't a good reason.

Community
  • 1
  • 1
Ray
  • 40,256
  • 21
  • 101
  • 138
  • Does `self` have better performance than what? – Mike Mackintosh Sep 17 '12 at 18:16
  • You've stated that you know they both do different things - those different things *are* why you might use one or the other. `self` will only return it's own class's static method/var even if it's been extended, where as `static` will return the extended method/var instead (if it has indeed been extended). – BenOfTheNorth Sep 17 '12 at 18:24
  • @BenGriffiths If I extend a class and override a non-final method or property, when I access it in a child class it should use my overridden items. I agree if someone believes they need to break fundamental this basic OOP principle, using `self` in this manner does provide a 'reason'. Still, that use case not a good reason, in fact it makes me wish it didn't exist at all--many noobs are still using self without understanding the unintended limits they're locking themselves into down the road. – Ray Sep 17 '12 at 18:56
  • @sixeightzero Since self is evaluated at op code generation time, not runtime it might take less time to evaluate self vs. static. I don't know if it does or doesn't, hence the question on stack overflow. – Ray Sep 17 '12 at 19:00
  • It's true it's scope for reasonable use is very limited with the latest versions of PHP, and other than what we've discussed I doubt there's any other reason to touch it. Being as it does provide a different purpose to static though I can see why it still exists (not just from a legacy standpoint). – BenOfTheNorth Sep 17 '12 at 19:22
  • I don't understand why people talk about the performance of microoptimizations in PHP - the language is one of the slowest in common use. 1% faster (or 10% on a completely contrived benchmark) than a slug is still a slug. – Sean McSomething Sep 17 '12 at 21:46
  • @SeanMcSomething If you don't understand something you probably shouldn't comment on it. – Ray Sep 17 '12 at 21:49

1 Answers1

3

Ah! Found my one good reason. At the moment in php 5.3 you cannot access compile time constants with static.

The code below will throw an error

 class Foo{
      const BAR = "FREE BEER";

      public static function whatDoWeLove(){
          return static::BAR;
      }
 }


 echo Foo::whatDoWeLove();

The error I get is:

PHP Fatal error:  "static::" is not allowed in compile-time constants in....

So, I guess for now self limps along until that changes or PHP adds the ability to use the final keyword with static properties. Drats.

Ray
  • 40,256
  • 21
  • 101
  • 138