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