<?php
可你知道self和static的区别么?其实区别很简单,只需要写几个demo就能懂:
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();
得到输出 This is a car model This is a car model可以发现,self在子类中还是会调用父类的方法
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();
得到输出
This is a car model This is a Taxi model可以看到,在调用static,子类哪怕调用的是父类的方法,但是父类方法中调用的方法还会是子类的方法(好绕嘴。。)