data-id="1190000005060322" data-license="sa">
In PHP object-oriented programming, you will always encounter
1 2 3 4 5 6 7 8 9 |
|
But do you know the difference between self and static?
In fact, the difference is very simple. You only need to write a few demos to understand:
Demo for self:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Get the output
1 2 3 |
|
You can find that self will still call the method of the parent class in the subclass
Demo for static
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
gets the output
1 2 3 |
|
You can see that when calling static
, even if the subclass calls the method of the parent class, the method called in the parent class method will still be the method of the subclass (so confusing. . )
Before PHP5.3, there was still a little difference between static and self. What exactly was it? After all, it was all in version 7. I won’t understand it anymore.
The summary is: self
can only reference methods in the current class, while the static
keyword allows functions to dynamically bind methods in the class at runtime.
Reference
http://www.programmerinterview.com/index.php/php-questions/php-self-vs-static/
The above introduces the difference between self and static in PHP object-oriented, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.