Questions tagged [method-chaining]

Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.

Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.

More Info

647 questions
189
votes
10 answers

PHP method chaining or fluent interface?

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?
Sanjay Khatri
  • 4,153
  • 7
  • 38
  • 42
188
votes
20 answers

Method chaining - why is it a good practice, or not?

Method chaining is the practice of object methods returning the object itself in order for the result to be called for another method. Like this: participant.addSchedule(events[1]).addSchedule(events[2]).setStatus('attending').save() This seems to…
Ilari Kajaste
  • 3,207
  • 2
  • 23
  • 25
63
votes
17 answers

Chaining Static Methods in PHP?

Is it possible to chain static methods together using a static class? Say I wanted to do something like this: $value = TestClass::toValue(5)::add(3)::subtract(2)::add(8)::result(); . . . and obviously I would want $value to be assigned the number…
Wilco
  • 32,754
  • 49
  • 128
  • 160
59
votes
4 answers

Conditional Builder Method Chaining Fluent Interface

I was wondering what would be the best way to implement a .When condition in a fluent interface using method chaining in a Builder object? For instance how would I implement the .WithSkill() and .When() methods in the following example: var level =…
Ken Burkhardt
  • 3,528
  • 6
  • 33
  • 45
57
votes
3 answers

Basic method chaining

I found this method chaining in python, but even with it I couldn't understand method chaining in Python. Here the goals are two: solve the coding problem and understand method chaining (given that I am still not 100% confident with callables). Down…
Pezze
  • 741
  • 1
  • 7
  • 14
57
votes
7 answers

How to chain method on a newly created object?

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?
aefxx
  • 24,835
  • 6
  • 45
  • 55
43
votes
6 answers

How to do method chaining in Java? o.m1().m2().m3().m4()

I've seen in many Java code notation that after a method we call another, here is an example. Toast.makeText(text).setGravity(Gravity.TOP, 0, 0).setView(layout).show(); As you see after calling makeText on the return we call setGravity and so…
Pentium10
  • 204,586
  • 122
  • 423
  • 502
40
votes
6 answers

Method Chaining in Java

While answering a few questions on here earlier and from some work I have been doing lately I have been wondering why Java does not support method chaining on its built in classes. If I were to create a Car class for example, I could make it…
Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
40
votes
5 answers

Method chaining + inheritance don’t play well together?

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) {…
Jason S
  • 184,598
  • 164
  • 608
  • 970
37
votes
3 answers

Is there a query method or similar for pandas Series (pandas.Series.query())?

The pandas.DataFrame.query() method is of great usage for (pre/post)-filtering data when loading or plotting. It comes particularly handy for method chaining. I find myself often wanting to apply the same logic to a pandas.Series, e.g. after having…
dmeu
  • 3,842
  • 5
  • 27
  • 43
34
votes
4 answers

Python equivalents to LINQ

In C#, with LINQ, if I have en enumeration enumerable, I can do: // a: Does the enumerable contain an item that satisfies the lambda? bool contains = enumerable.Any(lambda); // b: How many items satisfy the lambda? int count =…
31
votes
5 answers

How does basic object/function chaining work in javascript?

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…
Geuis
  • 41,122
  • 56
  • 157
  • 219
29
votes
4 answers

Return self in python

I have a class that represents object. And I have a bunch of methods which modify this object state with no obvious return or obviously without any return. In C# I would declare all these methods as void and see no alternatives. But in Python I am…
Philip B
  • 319
  • 1
  • 3
  • 9
29
votes
5 answers

method chaining in python

(not to be confused with itertools.chain) I was reading the following: http://en.wikipedia.org/wiki/Method_chaining My question is: what is the best way to implement method chaining in python? Here is my attempt: class chain(): def…
Rusty Rob
  • 16,489
  • 8
  • 100
  • 116
27
votes
4 answers

Chaining selectors in jQuery

I'm a guy used to mootools' way of chaining selectors, and I can't seem to find anywhere how to do the same in jQuery. Suppose I have a select element in the selectObj variable. What I need is to get the last option in that select. In mootools I…
andi
  • 14,322
  • 9
  • 47
  • 46
1
2 3
43 44