First commit

This commit is contained in:
Timothy Warren 2012-05-23 11:50:20 -04:00
parent d717c6303d
commit f604e0f04a
1 changed files with 79 additions and 0 deletions

79
JSObject.php Normal file
View File

@ -0,0 +1,79 @@
<?php
/**
* JSObject
*
* A PHP class to have an analoge to javascript's object literals
*
* @package JSObject
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/JSObject
*/
// --------------------------------------------------------------------------
/**
* JSObject class
*
* @package JSObject
*/
class JSObject {
/**
* Create the object
*
* @param mixed
*/
public function __construct($params = [])
{
foreach($params as $name => &$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