Created
April 10, 2018 11:31
-
-
Save wiyoe/0aaa540256e22df8f7a947194667e735 to your computer and use it in GitHub Desktop.
PHP method chaining ($this)
This file contains hidden or 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 | |
| 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