【发布时间】:2016-10-21 16:10:34
【问题描述】:
我想用它自己的父函数链接一个方法。
例子:
class Query
{
protected $limit;
/**
* Returns some third object that isn't in this family.
* This object represents the results, and also has
* a first function that gets called in a chain.
*/
public function get()
{
// Do Stuff
return new /* ... */;
}
public function take($amount)
{
$this->limit = $amount;
return $this;
}
}
class ChildQuery extends Query
{
protected $singular = false;
public function get()
{
if($this->singular)
return $this->take(1)->parent::get()->first();
return parent::get()
}
public function singular()
{
$this->singular = true;
return $this;
}
}
这显然不是完整的功能集,也不起作用,但你明白了。我希望ChildQuery::get 能够在链中调用Query::get。
现在,我必须这样做:
public function get()
{
$this->take(1);
parent::get()->first();
}
这对我没有吸引力。有什么想法吗?
如果重要的话,我正在运行 PHP 7。
我的最终结果应该是这样的:
$query->singular()->get(); // ($query is a ChildQuery)
【问题讨论】:
标签: php parent-child php-7 chaining