<?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,子類哪怕調用的是父類的方法,可是父類方法中調用的方法還會是子類的方法(好繞嘴。。)