Last active
August 29, 2015 14:05
-
-
Save wryk/109234809e6f4bfdbae9 to your computer and use it in GitHub Desktop.
reusable php stuff
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 | |
| trait Accessible { | |
| public function __get ($name) { | |
| $getter = 'get' . ucfirst($name); | |
| if (method_exists($this, $getter)) { | |
| return $this->$getter(); | |
| } else if (property_exists($this, $name)) { | |
| return $this->$name; | |
| } else { | |
| throw new Exception("Undefined property: " . get_class($this) . "::$" . $name); | |
| } | |
| } | |
| public function __set ($name, $value) { | |
| $setter = 'set' . ucfirst($name); | |
| if (method_exists($this, $getter)) { | |
| return $this->$setter($value); | |
| } else if (property_exists($this, $name)) { | |
| return $this->$name = $value; | |
| } else { | |
| throw new Exception("Undefined property: " . get_class($this) . "::$" . $name); | |
| } | |
| } | |
| public function __isset($name) { | |
| return isset($this->$name); | |
| } | |
| public function __unset($name) { | |
| unset($this->$name); | |
| } | |
| } |
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 | |
| trait Identifiable { | |
| protected $id; | |
| public function getId () { | |
| return $this->id; | |
| } | |
| } |
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 | |
| trait Singleton { | |
| static protected $instance; | |
| protected function __construct () {} | |
| protected function __clone () {} | |
| public static function getInstance () { | |
| if (!isset($self::instance)) { | |
| $self::instance = new $self(); | |
| } | |
| return $self::instance; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment