佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > PHP的new static和new self的區(qū)別與使用

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

熱門標(biāo)簽:阿里云 Linux服務(wù)器 Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù) 團(tuán)購網(wǎng)站 電子圍欄 科大訊飛語音識別系統(tǒng) 服務(wù)器配置

下面我們舉個栗子:

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 這個 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 會返回執(zhí)行 new static() 的類,比如 Son 執(zhí)行 get_class(Son::getStatic()) 返回的是 Son, Father 執(zhí)行 get_class(Father::getStatic()) 返回的是 Father

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

以上就是本文的全部內(nèi)容,希望對大家的學(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)簽:廣元 萍鄉(xiāng) 衢州 江蘇 衡水 大理 棗莊 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP的new static和new self的區(qū)別與使用》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    平湖市| 灵丘县| 南宁市| 赫章县| 三穗县| 九江市| 银川市| 雅江县| 青神县| 无极县| 大埔县| 嵊州市| 电白县| 加查县| 东阳市| 从化市| 鹰潭市| 惠来县| 革吉县| 吉林省| 平乡县| 寿宁县| 金平| 运城市| 牙克石市| 修水县| 宁武县| 九龙城区| 竹山县| 长垣县| 行唐县| 博罗县| 垣曲县| 封开县| 鲜城| 饶河县| 南涧| 大余县| 珠海市| 肥城市| 雷山县|