PHP Method Chaining or Fluent Interface

PHP method chaining or fluent interface?

It's rather simple, really. You have a series of mutator methods that all return the original (or other) object. 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->str .= "b";
return $this;
}

function getStr()
{
return $this->str;
}
}


$a = new fakeString();


echo $a->addA()->addB()->getStr();

This outputs "ab"

Try it online!

Can a Fluent Interface method in PHP detect where it is in the chain?

Your current plan is for each of your methods to perform an expensive HTTP call, but you are aware of the potential for significant delay to mount up if several methods are chained.

A good solution for this is to use chained methods to set the parameters and then to run an execute method at the end. So, you might modify your examples thus:

$var = $instance->user($id)->execute();
$var = $instance->user($id)->action($arg)->execute();

You'd have user() and action() returning $this (as well as changing the instance's state in some way) and then execute() would run the HTTP call in one go. This makes all calls prior to the last one very inexpensive indeed.

The return value could be $this, although a terminator method of this kind might just return a boolean success value. Your examples imply that a value is expected - having that relate to the last method prior to the execute() would probably work too.

Effects of method chaining

If you have to validate Something, i think it makes more sense to validate it in the AddRecipient Method itself, but the Performance should be about the same. And I'm not aware of any general disadvantages of using method chaining.

How do I chain methods in PHP?

To answer your cat example, your cat's methods need to return $this, which is the current object instance. Then you can chain your methods:

class cat {
function meow() {
echo "meow!";
return $this;
}

function purr() {
echo "purr!";
return $this;
}
}

Now you can do:

$kitty = new cat;
$kitty->meow()->purr();

For a really helpful article on the topic, see here: http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html

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

I agree that this is subjective. For the most part I avoid method chaining, but recently I also found a case where it was just the right thing - I had a method which accepted something like 10 parameters, and needed more, but for the most time you only had to specify a few. With overrides this became very cumbersome very fast. Instead I opted for the chaining approach:

MyObject.Start()
.SpecifySomeParameter(asdasd)
.SpecifySomeOtherParameter(asdasd)
.Execute();

The method chaining approach was optional, but it made writing code easier (especially with IntelliSense). Mind you that this is one isolated case though, and is not a general practice in my code.

The point is - in 99% cases you can probably do just as well or even better without method chaining. But there is the 1% where this is the best approach.



Related Topics



Leave a reply



Submit