关于 self 和static的区别

<?php
可你知道self和static的区别么?php

其实区别很简单,只须要写几个demo就能懂:get

Demo for self:
class Car
{
 public static function model(){
  self::getModel();
 }
 
 protected static function getModel(){
  echo "This is a car model";
 }
}io

Car::model();
Class Taxi extends Car
{
 protected static function getModel(){
  echo "This is a Taxi model";
 }
}function

Taxi::model();
获得输出    
This is a car model
This is a car modelclass

能够发现,self在子类中仍是会调用父类的方法model

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();demo

获得输出
This is a car model
This is a Taxi modelstatic

能够看到,在调用static,子类哪怕调用的是父类的方法,可是父类方法中调用的方法还会是子类的方法(好绕嘴。。)