本篇文章為大家展示了PHP中newstatic和newself的區(qū)別是什么,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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 則上面的基礎上,更聰明一點點: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)容就是PHP中newstatic和newself的區(qū)別是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。