Skip to content

Instantly share code, notes, and snippets.

@wiyoe
Created April 10, 2018 11:31
Show Gist options
  • Select an option

  • Save wiyoe/0aaa540256e22df8f7a947194667e735 to your computer and use it in GitHub Desktop.

Select an option

Save wiyoe/0aaa540256e22df8f7a947194667e735 to your computer and use it in GitHub Desktop.
PHP method chaining ($this)
<?php
class fakeString
{
private $str;
function __construct($str = null)
{
$this->str = $str;
}
function addA()
{
$this->str .= "a";
return $this;
}
function addB()
{
$this->str .= "b";
return $this;
}
function getStr()
{
return $this->str;
}
function returnThis(){
return $this;
}
}
$a = new fakeString("Hello,");
var_dump($a);
echo "<br />";
var_dump($a->returnThis());
echo "<br />";
echo $a->returnThis()->addA()->addB()->getStr();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment