I've seen many times Zend Framework using return $this;
pattern
style - and from my point of view:
我已经多次看到Zend Framework使用return $ this;模式风格 - 从我的角度来看:
-
Pro: seems its quite not bad
patternstyle for chaining many actions on the same object and making your code shorter.Pro:对于在同一个对象上链接许多操作并使代码更短,似乎是非常糟糕的模式样式。
-
Con: code looks a bit weird when you see that object returns itself in the method, which does something else (e.g. setter for some property)
Con:当你看到该对象在方法中返回自己时,代码看起来有点奇怪,这会做其他事情(例如某些属性的setter)
Is it really good
pattern
practice or maybe an anti-
pattern
practice?
它是真正好的模式实践还是反模式实践?
EDIT: well it was a little too much from my side to call it "pattern", thanks everyone for pointing me to right direction!
编辑:好吧,从我这边称它为“模式”有点太多了,谢谢大家指点我正确的方向!
5 个解决方案
#1
10
Returning this
allows you to chain calls and set values. It is very useful for configuring some object (see Fluent interface). You can express very easily what you want (and you can use different return types to achieve what you want).
返回此选项可以链接调用和设置值。它对配置某个对象非常有用(请参阅Fluent界面)。您可以非常轻松地表达您想要的内容(并且您可以使用不同的返回类型来实现您想要的效果)。
#2
7
I've found method chaining to be useful in circumstances where it makes sense; a domain specific language, for example:
我发现方法链在有意义的情况下很有用;特定于域的语言,例如:
$query->select('*')->from('users')->where(array('user_id' => 1, 'verified' => 1));
The thing is, these methods would only be returning void
anyway and so the return $this
merely functions as a short hand version of writing:
问题是,这些方法只会返回void,所以返回$ this只是作为写作的简写版本:
$query->select('*'); $query->from('users'); $query->where(...);
We're still going to be calling the toSQL()
or execute()
method to make use of the data we've populated our object with.
我们仍然会调用toSQL()或execute()方法来使用我们填充对象的数据。
In my opinion it is not an anti-pattern and can function as a legitimate, sensible, method of object population in the right circumstances.
在我看来,它不是一种反模式,可以在适当的情况下作为合法,合理的对象群体方法。
#3
7
If you mean "good practice or bad practice", here's my take:
如果你的意思是“良好做法或不良做法”,这是我的看法:
On the plus side, you get some syntactic sugar.
从好的方面来说,你会得到一些语法糖。
On the down side, you give up meaningful return values in favor of chainability. Which is not really feasible because eventually you will have to have methods which return something other than the base object, so you end up with some methods which are chainable and some which are not (users of your classes get to have fun guessing which ones are which.)
在不利方面,您放弃了有意义的返回值,转而支持可链接性。这是不可行的,因为最终你必须有方法返回基本对象以外的东西,所以你最终得到一些可链接的方法和一些不可链接的方法(你的类的用户可以有趣地猜测哪些是哪一个。)
Or you go full hog and make all of them chainable no matter what, but then you find yourself in ridiculous situations such as returning a fake "empty" object for the sake of preserving the chain, which object needs to be tested for some obscure property to determine if it's a "real" one or just a link in the chain.
或者你全力以赴,无论如何都可以将它们全部链接起来,但是你发现自己处于荒谬的境地,例如为了保护链条而返回一个虚假的“空”对象,这个对象需要进行一些模糊属性的测试确定它是“真实的”还是链中的链接。
The classical example is jQuery, which exhibits all the symptoms: the base object attempts to be the sole basic unit of data throughout the code (everything returns a jQuery object); fake object testing (if (obj.length)
); plus the self-contradiction where it still needs to break chainability for methods like getAttribute()
that return string.
经典的例子是jQuery,它展示了所有的症状:基础对象试图成为整个代码中唯一的基本数据单元(一切都返回一个jQuery对象);假对象测试(if(obj.length));加上自相矛盾,它仍然需要打破getAttribute()返回字符串等方法的可链接性。
IMHO, it's an awful mess to make of things just for the sake of that bit of syntactic sugar.
恕我直言,只是为了那些语法糖而制造东西是一件非常糟糕的事情。
#4
3
It is not exclusively PHP/Zend Framework doing this, as there are many other programming languages that use the fluent interface. I certainly think it comes in handy and that using the fluent interface is a good way of coding. Although sometimes codes looks weird, doesn't mean it is wrong and I don't think you can place this under con to be honest.
它不仅仅是PHP / Zend Framework这样做,因为有许多其他编程语言使用流畅的接口。我当然认为它派上用场,使用流畅的界面是一种很好的编码方式。虽然有时候代码看起来很奇怪,但并不意味着它是错误的,我认为你不能把它置于一致之下。
In the end the programmer only sees that he gets the same object back, not how it looks inside the code of the fluent interface class. I think the biggest pro in the fluent interface is the readability of the code. If you want to hear a con then debugging a fluent chain is one.
最后程序员只看到他得到了相同的对象,而不是它在流畅的接口类的代码中看起来如何。我认为流畅界面中最大的专业是代码的可读性。如果你想听到一个con,那么调试一个流畅的链是一个。
#5
2
This is called Fluent Interface, i don't think this is a pattern but better a way to implements function to reduce amount of code and improve readibility.
这称为Fluent Interface,我不认为这是一种模式,但更好的方法是实现函数以减少代码量并提高可读性。
I let you read the Wikipedia Page : http://en.wikipedia.org/wiki/Fluent_interface
我让你阅读*页面:http://en.wikipedia.org/wiki/Fluent_interface
#1
10
Returning this
allows you to chain calls and set values. It is very useful for configuring some object (see Fluent interface). You can express very easily what you want (and you can use different return types to achieve what you want).
返回此选项可以链接调用和设置值。它对配置某个对象非常有用(请参阅Fluent界面)。您可以非常轻松地表达您想要的内容(并且您可以使用不同的返回类型来实现您想要的效果)。
#2
7
I've found method chaining to be useful in circumstances where it makes sense; a domain specific language, for example:
我发现方法链在有意义的情况下很有用;特定于域的语言,例如:
$query->select('*')->from('users')->where(array('user_id' => 1, 'verified' => 1));
The thing is, these methods would only be returning void
anyway and so the return $this
merely functions as a short hand version of writing:
问题是,这些方法只会返回void,所以返回$ this只是作为写作的简写版本:
$query->select('*'); $query->from('users'); $query->where(...);
We're still going to be calling the toSQL()
or execute()
method to make use of the data we've populated our object with.
我们仍然会调用toSQL()或execute()方法来使用我们填充对象的数据。
In my opinion it is not an anti-pattern and can function as a legitimate, sensible, method of object population in the right circumstances.
在我看来,它不是一种反模式,可以在适当的情况下作为合法,合理的对象群体方法。
#3
7
If you mean "good practice or bad practice", here's my take:
如果你的意思是“良好做法或不良做法”,这是我的看法:
On the plus side, you get some syntactic sugar.
从好的方面来说,你会得到一些语法糖。
On the down side, you give up meaningful return values in favor of chainability. Which is not really feasible because eventually you will have to have methods which return something other than the base object, so you end up with some methods which are chainable and some which are not (users of your classes get to have fun guessing which ones are which.)
在不利方面,您放弃了有意义的返回值,转而支持可链接性。这是不可行的,因为最终你必须有方法返回基本对象以外的东西,所以你最终得到一些可链接的方法和一些不可链接的方法(你的类的用户可以有趣地猜测哪些是哪一个。)
Or you go full hog and make all of them chainable no matter what, but then you find yourself in ridiculous situations such as returning a fake "empty" object for the sake of preserving the chain, which object needs to be tested for some obscure property to determine if it's a "real" one or just a link in the chain.
或者你全力以赴,无论如何都可以将它们全部链接起来,但是你发现自己处于荒谬的境地,例如为了保护链条而返回一个虚假的“空”对象,这个对象需要进行一些模糊属性的测试确定它是“真实的”还是链中的链接。
The classical example is jQuery, which exhibits all the symptoms: the base object attempts to be the sole basic unit of data throughout the code (everything returns a jQuery object); fake object testing (if (obj.length)
); plus the self-contradiction where it still needs to break chainability for methods like getAttribute()
that return string.
经典的例子是jQuery,它展示了所有的症状:基础对象试图成为整个代码中唯一的基本数据单元(一切都返回一个jQuery对象);假对象测试(if(obj.length));加上自相矛盾,它仍然需要打破getAttribute()返回字符串等方法的可链接性。
IMHO, it's an awful mess to make of things just for the sake of that bit of syntactic sugar.
恕我直言,只是为了那些语法糖而制造东西是一件非常糟糕的事情。
#4
3
It is not exclusively PHP/Zend Framework doing this, as there are many other programming languages that use the fluent interface. I certainly think it comes in handy and that using the fluent interface is a good way of coding. Although sometimes codes looks weird, doesn't mean it is wrong and I don't think you can place this under con to be honest.
它不仅仅是PHP / Zend Framework这样做,因为有许多其他编程语言使用流畅的接口。我当然认为它派上用场,使用流畅的界面是一种很好的编码方式。虽然有时候代码看起来很奇怪,但并不意味着它是错误的,我认为你不能把它置于一致之下。
In the end the programmer only sees that he gets the same object back, not how it looks inside the code of the fluent interface class. I think the biggest pro in the fluent interface is the readability of the code. If you want to hear a con then debugging a fluent chain is one.
最后程序员只看到他得到了相同的对象,而不是它在流畅的接口类的代码中看起来如何。我认为流畅界面中最大的专业是代码的可读性。如果你想听到一个con,那么调试一个流畅的链是一个。
#5
2
This is called Fluent Interface, i don't think this is a pattern but better a way to implements function to reduce amount of code and improve readibility.
这称为Fluent Interface,我不认为这是一种模式,但更好的方法是实现函数以减少代码量并提高可读性。
I let you read the Wikipedia Page : http://en.wikipedia.org/wiki/Fluent_interface
我让你阅读*页面:http://en.wikipedia.org/wiki/Fluent_interface