Skip to content

Instantly share code, notes, and snippets.

@klhall1987
Last active October 27, 2015 14:33
Show Gist options
  • Save klhall1987/7ce4733540845c576805 to your computer and use it in GitHub Desktop.
Save klhall1987/7ce4733540845c576805 to your computer and use it in GitHub Desktop.
Method Chaining
<?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