Filantropy Posted December 6, 2013 Share Posted December 6, 2013 I made a class and used self:: in the constructor so it calls all methods directly. The problem is that when I tried the example in the book with pets it works perfect, but while doing something nearly the same, just with a computer class, it doesn't work right. I tried to override a method in a child class and it simply won't override it. I used static:: and it works perfect. I read the documentation on php.net but I don't seem to get the 'why' question. Why does it happen that self:: doesn't work, static:: works in this case? Why do I have to use late static binding? Link to comment Share on other sites More sharing options...
Antonio Conte Posted December 6, 2013 Share Posted December 6, 2013 As stated on PHP.net, self:: refers to the calling class. http://www.php.net/manual/en/language.oop5.late-static-bindings.php Limitations of self:: ¶Static references to the current class like self:: or __CLASS__ are resolved using the class in which the function belongs, as in where it was defined: Link to comment Share on other sites More sharing options...
Filantropy Posted December 6, 2013 Author Share Posted December 6, 2013 I don't really understand what is the calling class. Is that the class called? I only called the other two subclasses, not the superclass. Link to comment Share on other sites More sharing options...
Antonio Conte Posted December 6, 2013 Share Posted December 6, 2013 The calling class would be the class used to call the method from. Inside class Dog, self::prop; would equal Dog::prop etc. It's really hard to understand what you doing here without seeing your code, but my feeling is that your code shouldn't really be static. Static methods doesn't really allow for inheritance. The accepted answer in this Stack Overflow gives you some context: http://stackoverflow.com/questions/532754/inheritance-of-static-members-in-php Link to comment Share on other sites More sharing options...
Edward Posted December 20, 2013 Share Posted December 20, 2013 I love self even more than i love myself its extremely useful. Link to comment Share on other sites More sharing options...
Recommended Posts