Created
October 5, 2010 11:00
-
-
Save arieh/611358 to your computer and use it in GitHub Desktop.
This class is a proof of concept of how you can use the new Closures in PHP5.3 to import JS paradigms into 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 JSClass{ | |
/** | |
* @var array a container for all of the Class's members and methods. | |
*/ | |
private $params = array(); | |
/** | |
* @param array $params a list of members and methods to assign to the Class | |
* @access private | |
* @return JSClass instance | |
*/ | |
private function __construct(array $params){ | |
$this->params = $params; | |
return $this; | |
} | |
/** | |
* replaces the private constructor to make it more clear that this is a factory. | |
* | |
* @param array|JSClass $params a list of members and methods to assign to the Class | |
* @param array $ext a list of members and methods to use for overriding original methods (used when extending a class) | |
* | |
* @access public | |
* @return JSClass instance | |
*/ | |
static public function generate($params,$ext=array()){ | |
if (is_array($params)) | |
return new JSClass($params); | |
if ($params instanceof JSClass){ | |
$params = array_merge($params->params,$ext); | |
return new JSClass(); | |
} | |
} | |
/** | |
* calls a lambda, passing it $self as 1st parameter. Similar to JS's apply | |
* @param Closure $callback a method to run | |
* @param array $params an array of parameters to pass to the function | |
* @access public | |
* @return mixed whatever the method returns | |
*/ | |
public function apply($callback,$params = array()){ | |
//$self = &$this; | |
array_unshift($params,$this); | |
return call_user_func_array($callback,$params); | |
} | |
/** | |
* creates a new instance of the class | |
* | |
*/ | |
public function create(){ | |
$params = func_get_args(); | |
$obj = new JSClass($this->params); | |
if (array_key_exists('init',$this->params) && is_callable($this->params['init'])){ | |
$res = $obj->apply($this->params['init'],$params); | |
if ($res) return $res;//if initializer actualy returned a value | |
} | |
return $obj; | |
} | |
/** | |
* calls a method if exists in the parameter stack, using apply | |
*/ | |
public function __call($name,$params){ | |
$params = func_get_args(); //get all paramaters; | |
if (array_key_exists($name,$this->params) && is_callable($this->params[$name])){ | |
$this->apply($this->params[$name],$params); | |
} | |
} | |
public function __get($name){ | |
if (array_key_exists($name,$this->params)) return $this->params[$name]; | |
} | |
public function __set($name,$value){ | |
$this->params[$name]=$value; | |
} | |
public function __isset($name){ | |
return array_key_exists($name,$this->params); | |
} | |
} |
creating private variables is only possible JS style:
$Class3 = call_user_func(function() use ($Class1){
//closest thing we have to self-invoking until 5.4
$private = function($self){
$self->a = 'ccc';
};
$parent = &$Class1;
return = JSClass::generate($Class1, array(
'init' => function($self) use ($parent,$private){
$self->apply($private);
$self->apply($parent->init);
};
));
});
$Class3->create(); //ccc
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simple use example can be:
$Class1 = JSClass::generate(array(
'a' => 'aaa'
, 'init' => function($self){
echo $self->a;
}
));
Extending a class will be:
You can read more here: http://blog.arieh.co.il/posts/open/a-js-class-implementaion-for-php