new static與new self在PHP中有什么區(qū)別?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
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ǔ)上,更聰明一點點:static 會返回執(zhí)行 new static() 的類,比如 Son 執(zhí)行 get_class(Son::getStatic()) 返回的是 Son, Father 執(zhí)行 get_class(Father::getStatic()) 返回的是 Father
而在沒有繼承的情況下,可以認為 new self 和 new static 是返回相同的結(jié)果。
看完上述內(nèi)容,你們掌握new static與new self在PHP中有什么區(qū)別的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當前文章:newstatic與newself在PHP中有什么區(qū)別-創(chuàng)新互聯(lián)
文章起源:http://chinadenli.net/article34/cephse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、商城網(wǎng)站、做網(wǎng)站、小程序開發(fā)、微信公眾號、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容