Skip to content

Instantly share code, notes, and snippets.

@wryk
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save wryk/109234809e6f4bfdbae9 to your computer and use it in GitHub Desktop.

Select an option

Save wryk/109234809e6f4bfdbae9 to your computer and use it in GitHub Desktop.
reusable php stuff
<?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);
}
}
<?php
trait Identifiable {
protected $id;
public function getId () {
return $this->id;
}
}
<?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