php面向?qū)ο缶幊蘳elf和static的區(qū)別
在php的面向?qū)ο缶幊讨?,總會遇?/p>
class test{
public static function test(){
self::func();
static::func();
}
public static function func(){}
}
可你知道self和static的區(qū)別么?
其實區(qū)別很簡單,只需要寫幾個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
可以發(fā)現(xiàn),self在子類中還是會調(diào)用父類的方法
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
可以看到,在調(diào)用static,子類哪怕調(diào)用的是父類的方法,但是父類方法中調(diào)用的方法還會是子類的方法(好繞嘴。。)
在PHP5.3版本以前,static和self還是有一點區(qū)別,具體是什么,畢竟都是7版本的天下了。就不去了解了。
總結(jié)呢就是:self只能引用當(dāng)前類中的方法,而static關(guān)鍵字允許函數(shù)能夠在運行時動態(tài)綁定類中的方法。
- php self,$this,const,static,->的使用
- php類中的$this,static,final,const,self這幾個關(guān)鍵字使用方法
- PHP中static關(guān)鍵字以及與self關(guān)鍵字的區(qū)別
- PHP new static 和 new self詳解
- PHP中new static()與new self()的比較
- PHP編程過程中需要了解的this,self,parent的區(qū)別
- php class中self,parent,this的區(qū)別以及實例介紹
- 探討PHP中this,self,parent的區(qū)別詳解
- PHP5中的this,self和parent關(guān)鍵字詳解教程
- PHP面向?qū)ο蟪绦蛟O(shè)計中的self、static、parent關(guān)鍵字用法分析
相關(guān)文章
php7 參數(shù)、整形及字符串處理機(jī)制修改實例分析
這篇文章主要介紹了php7 參數(shù)、整形及字符串處理機(jī)制修改,結(jié)合實例形式分析了php7 參數(shù)、整形及字符串處理機(jī)制較舊版本的區(qū)別及相關(guān)操作注意事項,需要的朋友可以參考下2020-05-05
PHP創(chuàng)建word文檔的方法(平臺無關(guān))
這篇文章主要介紹了PHP創(chuàng)建word文檔的方法,結(jié)合實例形式分析了與平臺無關(guān)的生成word文檔的方法,非常簡單實用,需要的朋友可以參考下2016-03-03
PHP實現(xiàn)的增強(qiáng)性mhash函數(shù)
這篇文章主要介紹了PHP實現(xiàn)的增強(qiáng)性mhash函數(shù),使用默認(rèn)mhash函數(shù)時報錯,找到了兩個解決方法,需要的朋友可以參考下2015-05-05

