Eni*_*lus 0 php syntax frameworks
我最近看到一些样本PHP代码看起来像这样:
$myObj->propertyOne = 'Foo'
->propertyTwo = 'Bar'
->MethodA('blah');
Run Code Online (Sandbox Code Playgroud)
相反:
$myObj->propertyOne = 'Foo';
$myObj->propertyTwo = 'Bar';
$myObj->MethodA('blah');
Run Code Online (Sandbox Code Playgroud)
这是来自特定框架还是PHP的特定版本,因为我从未见过它的工作原理?
你看到的是fluent interface,但你的代码示例是错误的.长话短说,fluent setter应该返回$this:
class TestClass {
private $something;
private $somethingElse;
public function setSomething($sth) {
$this->something = $sth;
return $this;
}
public function setSomethingElse($sth) {
$this->somethingElse = $sth;
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
$sth = new TestClass();
$sth->setSomething(1)
->setSomethingElse(2);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
98 次 |
| 最近记录: |