ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
``` self就是写在哪个类里面, 实际调用的就是这个类. static代表使用的这个类(当前是实例化的那个类), 就是你在父类里写的static,然后被子类覆盖,使用的就是子类的方法或属性 大概意思是说self调用的就是本身代码片段这个类,而static调用的是从堆内存中提取出来,访问的是当前实例化的那个类,那么 static 代表的就是那个类,例子比较容易明白些。 -------------------------------------------------------------------------------- class Person { public static function name() { echo "Person的name方法<br />"; } public static function callself() { self::name(); //php高版本过时建议使用 //(new self())->name(); } public static function callstatic() { static::name(); } } class Man extends Person { //重写父类方法 public static function name() { echo "Man的name方法<br />"; } public static function callparent() { parent::name(); } } Man::callself(); //Person的name方法 Man::callstatic(); //Man的name方法 Man::callparent(); //Person的name方法 Person::callself(); //Person的name方法 Person::callstatic(); //Person的name方法 Person::callparent(); //致命错误 parent必须写到子类方法里 ```