在php的面向对象编程中,总会遇到php
class test{ public static function test(){ self::func(); static::func(); } public static function func(){} }
可你知道self和static的区别么?编程
其实区别很简单,只须要写几个demo就能懂:函数
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();
获得输出code
This is a car model This is a car model
能够发现,self在子类中仍是会调用父类的方法对象
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
This is a car model This is a Taxi model
能够看到,在调用static
,子类哪怕调用的是父类的方法,可是父类方法中调用的方法还会是子类的方法(好绕嘴。。)io
在PHP5.3版本之前,static和self仍是有一点区别,具体是什么,毕竟都是7版本的天下了。就不去了解了。面向对象编程
总结呢就是:self
只能引用当前类中的方法,而static
关键字容许函数可以在运行时动态绑定类中的方法。function
参考class