本文介紹了“返回 $this"是什么意思?意思是?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我正在嘗試?yán)斫膺@段代碼,當(dāng)我到達(dá)最后一行時(shí),我沒(méi)有理解.:(
I'm trying to understand this code, and when I arrived at the final line, I didn't get it. :(
我可以請(qǐng)你幫忙看看,return $this
是什么意思?
Can I have your help in order to find out, what does return $this
mean ?
public function setOptions(array $options) {
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
//???? - return what ?
return $this;
}
更新:
為了更好地說(shuō)明,我刪除了我的評(píng)論.
Update:
I've removed my comments for better clarification.
推薦答案
這種編碼方式被稱為 流暢的界面.return $this
返回當(dāng)前對(duì)象,所以你可以這樣寫代碼:
This way of coding is called fluent interface. return $this
returns the current object, so you can write code like this:
$object
->function1()
->function2()
->function3()
;
代替:
$object->function1();
$object->function2();
$object->function3();
這篇關(guān)于“返回 $this"是什么意思?意思是?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!