开发者

What is the purpose of return'ing $this when setting vars in the model

I am a self thought hobby programmer and therefore don't have the fundamentals always down the way you professionals do. So please excuse me if this is basic.

What is the purpose or benefit of return'ing $this when setting vars in the model. I have seen this done in other places too bu开发者_运维技巧t cant figure this out.

Sample code:

public function setAlias($Alias){
    $this->_Alias = (string) $Alias;
    return $this;
}

public function getAlias(){
    return $this->_Alias;
}


It allows you to do method chaining. For example,

$object = new Object();
$object->setAttribute1("value")->setAttribute2("value")


It allows method chaining:

$someObj->method1()->method2();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜