[PHP] Method chaining

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
It seems method chaining has become more popular lately and a lot of people wonder how to do it, this simple tutorial will show you how.

PHP:
<?php
class Animal { //give the class a name
    public function Dog() { //method
        echo 'Woof';
        return $this; //return Animal (this is basically what makes it chainable)
    }
   
    public function Cat() {
        echo 'Meow';
        return $this;
    }
   
    public function Sheep() {
        echo 'Baa';
        return $this;
    }
}

$animal = new Animal();
$animal->Dog()->Cat()->Sheep();
?>

The outcome would then become:
WoofMeowBaa

You can also reuse the methods in a single chain, such as:
PHP:
$animal->Dog()->Cat()->Dog()->Dog()->Sheep();

Most people use this for eye-candy but it also makes code easier to read if coded properly.

Hope it helped,
Mark
 

IntactDev

Member
Nov 22, 2012
399
71
Always wondered how I would accomplish this; kudos for this!

Also, to make it seem a little bit cleaner, you can format it like this:
PHP:
$animal
    ->Dog()
    ->Cat()
    ->Dog()
    ->Sheep();
This way, you can see the functions a little bit better, allowing easier edits.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Always wondered how I would accomplish this; kudos for this!

Also, to make it seem a little bit cleaner, you can format it like this:
PHP:
$animal
    ->Dog()
    ->Cat()
    ->Dog()
    ->Sheep();
This way, you can see the functions a little bit better, allowing easier edits.
True, but it's more lines and spacing that make the file slightly larger and takes longer for PHP to parse ;p
 

Adil

DevBest CEO
May 28, 2011
1,276
714
Maybe so, but say you do the multi-line-chaining several times in one script, they'll all add up.
It also reduces the amount of times you need to call a variable :D
Code:
//pseudocode
var = new A();
//without method chaining
a.bark();
a.woof();

//with it
a.bark().woof();
 

academic

Member
Nov 17, 2011
31
6
Always wondered how I would accomplish this; kudos for this!

Also, to make it seem a little bit cleaner, you can format it like this:
PHP:
$animal
    ->Dog()
    ->Cat()
    ->Dog()
    ->Sheep();
This way, you can see the functions a little bit better, allowing easier edits.
That shouldn't be done. It should be constructed "$animal->Dog()->Cat()->Sheep();" it is still able to be read just as fine and is clean enough. It is unnecessary and things like this can get people carried away and thus change it even more and next minute we have an entire different syntax. :>
 

academic

Member
Nov 17, 2011
31
6
I gave up on life when you said:
I don't really care. That was stupid and silly to even mention. However, I'm quite serious when I say that it's users like yourself, who say "oh, let's change it because it doesn't look neat, and it's not easy to read, uhh, let's change it". Don't you think maybe, just for a little bit, think maybe this may influence people to use your example, they start changing it.

By the way, with your witty response, their are some things that you can do in PHP that do not generate an error and will continue without any errors being thrown, so syntax does not always have to be perfect. By changing the view of code, it confuses new people to PHP and bottom line it's just unnecessary, like @m0nsta. has said it slows down the PHP parser.
 

Users who are viewing this thread

Top