method-chaining

How to chain method on a newly created object?

不想你离开。 提交于 2019-11-26 04:48:18
问题 I would like to know whether there\'s a way to chain methods on a newly created object in PHP? Something like: class Foo { public function xyz() { ... return $this; } } $my_foo = new Foo()->xyz(); Anyone know of a way to achieve this? 回答1: In PHP 5.4+, the parser's been modified so you can do something like this (new Foo())->xyz(); Wrap the instantiation in parenthesis, and chain away. Prior to PHP 5.4, when you're using the new Classname(); syntax, you can't chain a method call off the

How does basic object/function chaining work in javascript?

坚强是说给别人听的谎言 提交于 2019-11-26 03:19:41
问题 I\'m trying to get the principles of doing jQuery-style function chaining straight in my head. By this I mean: var e = f1(\'test\').f2().f3(); I have gotten one example to work, while another doesn\'t. I\'ll post those below. I always want to learn the first principle fundamentals of how something works so that I can build on top of it. Up to now, I\'ve only had a cursory and loose understanding of how chaining works and I\'m running into bugs that I can\'t troubleshoot intelligently. What I

Method chaining + inheritance don’t play well together?

北慕城南 提交于 2019-11-26 02:34:05
问题 This question has been asked in a C++ context but I\'m curious about Java. The concerns about virtual methods don\'t apply (I think), but if you have this situation: abstract class Pet { private String name; public Pet setName(String name) { this.name = name; return this; } } class Cat extends Pet { public Cat catchMice() { System.out.println(\"I caught a mouse!\"); return this; } } class Dog extends Pet { public Dog catchFrisbee() { System.out.println(\"I caught a frisbee!\"); return this; }

PHP method chaining?

拟墨画扇 提交于 2019-11-25 22:57:40
问题 I am using PHP 5 and I\'ve heard of a new featured in the object-oriented approach, called \'method chaining\'. What is it exactly? How do I implement it? 回答1: Its rather simple really, you have a series of mutator methods that all returns the original (or other) objects, that way you can keep calling methods on the returned object. <?php class fakeString { private $str; function __construct() { $this->str = ""; } function addA() { $this->str .= "a"; return $this; } function addB() { $this-