Data-id= "1190000005060322" data-license= "sa" >
In the object-oriented programming of PHP, you will always encounter
class test{ public static function test(){ self::func(); static::func(); } public static function func(){}}
But do you know the difference between self and static?
In fact, the difference is very simple, just need to write a few demo can understand:
Demo for self:
class Car{ public static function model(){ self::getModel(); } protected static function getModel(){ echo "This is a car model"; }}Car::model();Class Taxi extends Car{ protected static function getModel(){ echo "This is a Taxi model"; }}Taxi::model();
Get output
This is a car modelThis is a car model
It can be found that self in a subclass still calls the parent class's method
Demo for static
class Car{ public static function model(){ static::getModel(); } protected static function getModel(){ echo "This is a car model"; }}Car::model();Class Taxi extends Car{ protected static function getModel(){ echo "This is a Taxi model"; }}Taxi::model();
Get output
This is a car modelThis is a Taxi model
As you can see, in a call static
, a subclass even calls a method of the parent class, but the method called in the parent class method is also a method of the subclass (good Raozui. )
Before the PHP5.3 version, static and self still have a little difference, specifically what, after all, is the 7 version of the world. Don't get to know it.
The summary is that self
only the methods in the current class can be referenced, and the static
keyword allows the function to dynamically bind the methods in the class at run time.
Reference
http://www.programmerinterview.com/index.php/php-questions/php-self-vs-static/
The above describes the PHP object-oriented self and static differences, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.