Last active
August 29, 2015 13:56
-
-
Save schleumer/9009070 to your computer and use it in GitHub Desktop.
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 Users implements JsonSerializable{ | |
private $_data = array(); | |
public function addUser(User $user){ | |
$this->_data[] = $user; | |
} | |
public function jsonSerialize(){ | |
return $this->_data; | |
} | |
public function __toString(){ | |
return json_encode($this->jsonSerialize()); | |
} | |
} | |
class User implements JsonSerializable { | |
private $_name = null; | |
public function getName(){ | |
return $this->_name; | |
} | |
public function setName($value){ | |
$this->_name = $value; | |
} | |
public function jsonSerialize(){ | |
return [ | |
"name" => $this->_name | |
]; | |
} | |
} | |
$users = new Users(); | |
$user = new User(); | |
$user->setName('Carinha dahora'); | |
$users->addUser($user); | |
echo $users; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment