简体   繁体   English

方法链接中的“管道”方法

[英]“Piping” methods in methods chaining

Lately I implemented this behavior for yii: https://github.com/garex/yii-pipe-behavior 最近我为yii实现了此行为: https : //github.com/garex/yii-pipe-behavior

It's main purpose is to allow methods chaining for methods, that are getters. 它的主要目的是允许方法链接为吸气剂的方法。 Something in such style could be implemented in any other language/framework. 这种风格的东西可以用任何其他语言/框架来实现。 It's more like syntactic sugar for fanats of method chaining. 它更像是方法链式方法的语法糖。

From readme: 从自述文件:

For example owner has method gimmeAll , that returns array that we want to transform by another owner`s method, let it be toSomething . 例如owner具有方法gimmeAll ,该方法返回要由另一个所有者的方法转换的数组,将其设为toSomething In old style we call: 在旧样式中,我们称:

 $bla = Something::create()->toSomething(Something::create()->one()->two()->three()->gimmeAll()); 

But with this behavior we can do this in more elegant way: 但是通过这种行为,我们可以以更优雅的方式做到这一点:

 $bla = Something::create()->one()->two()->three()->pipe('gimmeAll')->unpipe('toSomething', '{r}'); 

If unpiped method has single parameter, then we can omit '{r}' parameter and call it like: 如果unpiped方法具有单个参数,那么我们可以省略'{r}'参数,并按如下方式调用它:

 $bla = Something::create()->one()->two()->three()->pipe('gimmeAll')->unpipe('toSomething'); 

So my questions are: 所以我的问题是:

  1. Can it be really useful? 真的有用吗? I implemented this stuff in late night and still not sure. 我在深夜实施了这些东西,但仍然不确定。

  2. Could it be a "bicycle"? 会是“自行车”吗? May be such stuff exists in another lang/framework? 这样的东西可能存在于另一个语言/框架中吗?

Based on the results and answers from thread at yii forum 根据yii论坛中线程的结果和答案

No, it's not usefult and even superfluous. 不,它没有用,甚至是多余的。 Some quotes from there: 那里的一些报价:

I think saving results in a variable and passing it to the other method is much cleaner, readable, better supported by IDE's and just more sane. 我认为将结果保存到变量中并将其传递给另一种方法更简洁,易读,并得到IDE的更好支持,并且更加理智。

class Something
{
        public function gimmeAllToSomething()
        {
                return $this->toSomething($this->gimmeAll());
        }
}

$bla = Something::create()->one()->two()->tree()->gimmeAllToSomething();

It's a bit more code to type and test, but best programming practices aren't about typing less. 键入和测试的代码要多一些,但是最佳编程实践并不是要减少打字量。


Currently in real scenario I also used gimmeAllToSomething() approach. 目前,在实际场景中,我还使用了gimmeAllToSomething()方法。 So we can think about it as a door where you do not need to go. 因此,我们可以将其视为不需要走的门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM