Created
February 24, 2016 09:11
-
-
Save kirilkirkov/d333f77df4f33d76bd5b to your computer and use it in GitHub Desktop.
php fluent interface pattern $obj->method1()->method2();
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 | |
class User | |
{ | |
protected $name; | |
protected $surname; | |
protected $password; | |
public function setName($name) | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
public function setSurname($surname) | |
{ | |
$this->surname = $surname; | |
return $this; | |
} | |
public function setPassword($password) | |
{ | |
if (!$password) { | |
throw new InvalidArgumentException("Password shouldn't be empty"); | |
} | |
$this->password = $password; | |
return $this; | |
} | |
} | |
$user = new User; | |
$user->setName('John')->setSurname('Doe')->setPassword('root'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment