Created
July 21, 2011 07:11
-
-
Save shesek/1096705 to your computer and use it in GitHub Desktop.
Chainable wrapper for PHP
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 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