最近我为yii实现了这种行为:https://github.com/garex/yii-pipe-behavior
它的主要目的是允许方法链接方法,即getter。这种风格的东西可以用任何其他语言/框架实现。对于方法链的狂热者来说,它更像是语法糖。
自述文件:
例如,owner拥有方法 gimmeAll ,它返回我们的数组 想要通过另一个所有者的方法进行转换,让它成为 toSomething 。 我们称之为旧式:
$bla = Something::create()->toSomething(Something::create()->one()->two()->three()->gimmeAll());
但是通过这种行为,我们可以更优雅的方式做到这一点:
$bla = Something::create()->one()->two()->three()->pipe('gimmeAll')->unpipe('toSomething', '{r}');
如果unpiped方法有单个参数,那么我们可以省略' {r}' 参数并称之为:
$bla = Something::create()->one()->two()->three()->pipe('gimmeAll')->unpipe('toSomething');
它真的有用吗?我在深夜实施了这些东西 仍然不确定。
可能是"自行车"?可能是这样的东西存在于另一个lang / framework中吗?
答案 0 :(得分:0)
不,这不是有用的,甚至是多余的。一些引用来自那里:
我认为将结果保存在变量中并将其传递给其他方法是很多的 更干净,可读,更好地支持IDE,更加理智。
class Something
{
public function gimmeAllToSomething()
{
return $this->toSomething($this->gimmeAll());
}
}
$bla = Something::create()->one()->two()->tree()->gimmeAllToSomething();
输入和测试的代码要多一些,但最好的编程实践并不是打字少。
目前在实际场景中我还使用了gimmeAllToSomething()方法。因此,我们可以将其视为您不需要去的门。