Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // once you add the magical setter and getter
- // you can can access any private fields as usual
- // but you can also develop custom setter and getter if you want
- $user = new MagicUser();
- $user->username = "test";
- $user->password = "pass";
- echo $user->username; // will print Test not test.
- // magical setter and getter
- class MagicUser{
- private $username;
- private $password;
- public function setUsername($username){
- $this->username = ucfirst($username);
- }
- public function __set($name , $value){
- $setter = 'set'.ucfirst($name);
- if(method_exists($this,$setter)){
- $this->$setter($value);
- }else{
- $this->$name = $value;
- }
- }
- public function __get($name){
- $getter = 'get'.ucfirst($name);
- if(method_exists($this,$getter)){
- return $this->$getter();
- }else{
- return $this->$name;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement