国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP中new static() 和 new self() 的區(qū)別介紹

2020-03-22 17:51:18
字體:
供稿:網(wǎng)友
長夜漫漫啊!今天領(lǐng)導(dǎo)本地搭建一個站。發(fā)現(xiàn)用PHP 5.2 搭建不起來,站PHP代碼里面有很多5.3以上的部分,領(lǐng)導(dǎo)讓苦逼我更改在5.2下能運(yùn)行。改著改著發(fā)現(xiàn)了一個地方
復(fù)制代碼 代碼如下:
return new html' target='_blank'>static($val);
這尼瑪是神馬,只見過
復(fù)制代碼 代碼如下:
return new self($val);
于是上網(wǎng)查了下,他們兩個的區(qū)別。self – 就是這個類,是代碼段里面的這個類。static – PHP 5.3加進(jìn)來的只得是當(dāng)前這個類,有點(diǎn)像$this的意思,從堆內(nèi)存中提取出來,訪問的是當(dāng)前實(shí)例化的那個類,那么 static 代表的就是那個類。還是看看老外的專業(yè)解釋吧。self refers to the same class whose method the new operation takes place in.static in PHP 5.3's late static bindings refers to whatever class in the hierarchy which you call the method on.In the following example, B inherits both methods from A. self is bound to A because it's defined in A‘s implementation of the first method, whereas static is bound to the called class (also see get_called_class() ).
復(fù)制代碼 代碼如下:
class A {
public static function get_self() {
return new self();
} public static function get_static() {
return new static();
}
}class B extends A {}echo get_class(B::get_self()); // A
echo get_class(B::get_static()); // B
echo get_class(A::get_static()); // A
這個例子基本上一看就懂了吧。原理了解了,但是問題還沒有解決,如何解決掉 return new static($val); 這個問題呢?其實(shí)也簡單就是用 get_class($this); 如下
復(fù)制代碼 代碼如下:
class A {
public function create1() {
$class = get_class($this);
return new $class();
}
public function create2() {
return new static();
}
}class B extends A {}$b = new B();
var_dump(get_class($b- create1()), get_class($b- create2()));/*
The result
string(1) "B"
string(1) "B"
*/
PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌苏市| 前郭尔| 榆社县| 镇坪县| 凌海市| 祁连县| 兴安县| 江油市| 辽中县| 治县。| 贵德县| 阿勒泰市| 平罗县| 青州市| 漳州市| 东台市| 静海县| 东乌珠穆沁旗| 抚顺市| 樟树市| 东莞市| 许昌市| 铁岭市| 蓬莱市| 玉溪市| 特克斯县| 贵州省| 萨嘎县| 九龙城区| 黄石市| 南安市| 靖宇县| 衡南县| 兴义市| 泰兴市| 渝中区| 新密市| 彭山县| 无极县| 托里县| 巴塘县|