Last active
October 27, 2015 14:33
-
-
Save klhall1987/7ce4733540845c576805 to your computer and use it in GitHub Desktop.
Method Chaining
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Example of single class method chaining | |
class stringExample | |
{ | |
private $str; | |
function __construct() | |
{ | |
$this->str = ''; | |
} | |
function addFoo() | |
{ | |
$this->str .= 'Foo'; | |
return $this; | |
} | |
function addBar() | |
{ | |
$this->str .= 'Bar'; | |
return $this; | |
} | |
function addSpace() | |
{ | |
$this->str .= ' '; | |
return $this; | |
} | |
function getStr() | |
{ | |
return $this->str; | |
} | |
} | |
$baz = new stringExample(); | |
echo $baz->addFoo()->addSpace()->addBar()->getStr(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment