From f604e0f04a5c7d7cf4b37043fa3b3284b9926b7b Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 23 May 2012 11:50:20 -0400 Subject: [PATCH] First commit --- JSObject.php | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 JSObject.php diff --git a/JSObject.php b/JSObject.php new file mode 100644 index 0000000..4381f22 --- /dev/null +++ b/JSObject.php @@ -0,0 +1,79 @@ + &$val) + { + // Bind '$this' for closures + if ($val instanceof Closure) + { + $val->bindTo($this); + } + + // Add the parameter to the object + $this->$name = $val; + } + } + + /** + * Magic method to invoke appended closures + * + * @param string + * @param array + * @return mixed + */ + public function __call($name, $params = []) + { + if (is_callable($this->$name)) + { + return call_user_func_array($this->$name, $params); + } + + return NULL; + } + + /** + * Treat invokation of the object as creating a new object + * + * @param mixed + * @return JSObject + */ + public function __invoke($params = []) + { + $class = __CLASS__; + + // Create the new object + $obj = new $class(); + + // Pass the parameters to the constructor of the new object + $obj = call_user_func_array([$obj, '__construct'], $params); + + return $obj; + } +} + +// End of JSObject.php \ No newline at end of file