Skip to content

Instantly share code, notes, and snippets.

@shesek
Created July 21, 2011 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shesek/1096705 to your computer and use it in GitHub Desktop.
Save shesek/1096705 to your computer and use it in GitHub Desktop.
Chainable wrapper for PHP
<?php
class Chainable {
private $obj;
private $result;
public function __construct($obj){
$this->obj = $obj;
}
public function __call($method, $args) {
if (substr($method, 0, 1) == '_') {
array_unshift($args, $this->result);
$method = substr($method, 1);
}
$this->result = call_user_func_array(array($this->obj, $method), $args);
return $this;
}
public function __set($key, $value){$this->obj->$key = $value; return $this;}
public function __get($key){return $this->obj->$key;}
public function result(){
return $this->result;
}
public static function get($obj){
$chainable = new Chainable($obj);
return $chainable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment