2014-01-29 70 views
1

当教条2 autogenerating实体,所有制定者获得回报$这种说法是这样的:学说意义

class Foo{ 
    //... skipping details .... 

    public function setFoo(\Application\Entity\SomeEntity $someValue){ 
     $this->someValue = $someValue; 
     return $this; 
    } 
} 

我的问题是,我为什么要返回$实例当我已经有使用$foo = new foo();的实例时?这背后有什么想法?这是一种设计模式吗?

回答

2

使用“链接方法”。像:

$foo->setFoo()->setFoo1()->setFoo... 

它被称为Fluent Interface

这个想法是,当方法必须返回$this时,您可以在返回的$this上调用此类的其他方法。在ORM中,例如,用于构建sql查询:$this->select()->from()->where()->....