静态绑定self和static的区别 发表于 2019-05-07 self示例代码123456789101112131415161718192021222324252627282930313233class Father{ public function __construct() { $this->init(); self::test(); } public function init() { echo 'father init...<br>'; } public static function test() { echo "father test....<br>"; } }class Son extends Father{ public function init() { echo 'son init..<br>'; } public static function test() { echo "son test..<br>"; }}$son_obj = new Son(); 运行结果12son init..father test.... 从运行结果看,调用init非静态方法,父类的方法已经被子类覆盖了;但使用self调用test静态方法,使用的还是父类的静态方法。static示例代码123456789101112131415161718192021222324252627282930313233class Father{ public function __construct() { $this->init(); static::test(); } public function init() { echo 'father init...<br>'; } public static function test() { echo "father test....<br>"; } }class Son extends Father{ public function init() { echo 'son init..<br>'; } public static function test() { echo "son test..<br>"; }}$son_obj = new Son(); 运行结果12son init..son test.. 使用static调用静态方法使用了当前分类的静态方法小结简单通俗的来说,self就是写在哪个类里面,实际调用的就是这个类;static代表使用的这个类, 就是你在父类里写的static,然后被子类覆盖,使用的就是子类的方法或属性。Ps:self指向本类,是类内指针;static指向使用类。