濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > PHP的new static和new self的區(qū)別與使用

PHP的new static和new self的區(qū)別與使用

熱門標(biāo)簽:天潤(rùn)融通外呼系統(tǒng)好嗎 高德地圖標(biāo)注位置怎么標(biāo)注 欣思維地圖標(biāo)注 泊頭在哪里辦理400電話 江門回?fù)芡夂粝到y(tǒng) 杭州語音電銷機(jī)器人 高德地圖標(biāo)注店鋪收費(fèi)嗎 電銷機(jī)器人沒有效果怎么樣 江西電銷機(jī)器人收費(fèi)

下面我們舉個(gè)栗子:

class Father {
  public static function getSelf() {
    return new self();
  }

  public static function getStatic() {
    return new static();
  }
}

class Son extends Father {}

echo get_class(Son::getSelf()); // Father
echo get_class(Son::getStatic()); // Son
echo get_class(Father::getSelf()); // Father
echo get_class(Father::getStatic()); // Father

new self

這里面注意這一行 get_class(Son::getStatic()); 返回的是 Son 這個(gè) class, 可以總結(jié)如下:
self 返回的是 new self 中關(guān)鍵字 new 所在的類中,比如這里例子的 :

public static function getSelf() {
  return new self(); // new 關(guān)鍵字在 Father 這里
}

始終返回 Father。

new static

static 則上面的基礎(chǔ)上,更聰明一點(diǎn)點(diǎn):static 會(huì)返回執(zhí)行 new static() 的類,比如 Son 執(zhí)行 get_class(Son::getStatic()) 返回的是 Son, Father 執(zhí)行 get_class(Father::getStatic()) 返回的是 Father

而在沒有繼承的情況下,可以認(rèn)為 new self 和 new static 是返回相同的結(jié)果。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP面向?qū)ο蟪绦蛟O(shè)計(jì)中的self、static、parent關(guān)鍵字用法分析
  • php面向?qū)ο蟪绦蛟O(shè)計(jì)中self與static的區(qū)別分析
  • PHP面向?qū)ο笾衝ew self()與 new static()的區(qū)別淺析
  • 淺談PHP中new self()和new static()的區(qū)別
  • PHP Class self 與 static 異同與使用詳解

標(biāo)簽:雙鴨山 石嘴山 內(nèi)江 深圳 駐馬店 大同 江門 巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP的new static和new self的區(qū)別與使用》,本文關(guān)鍵詞  PHP,的,new,static,和,self,區(qū)別,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。

  • 相關(guān)文章
  • 下面列出與本文章《PHP的new static和new self的區(qū)別與使用》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于PHP的new static和new self的區(qū)別與使用的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    阳原县| 辽阳市| 通化市| 西充县| 滦南县| 吐鲁番市| 淳安县| 额尔古纳市| 鄄城县| 新余市| 资讯 | 丹凤县| 怀集县| 桂林市| 南通市| 洱源县| 襄垣县| 屏南县| 温宿县| 通州市| 保康县| 巴南区| 万山特区| 巨鹿县| 娄烦县| 青冈县| 桓台县| 文登市| 兴和县| 霍林郭勒市| 宣城市| 吴川市| 新化县| 合肥市| 河曲县| 兴隆县| 丹棱县| 青冈县| 汨罗市| 边坝县| 柏乡县|