-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunc.php
49 lines (44 loc) · 1.14 KB
/
Func.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* Class description
*
* @author Safouane Ahmed Salah
* @license MIT
*/
namespace React;
abstract class Func extends Component{
/**
* Render the class that represent the html tag
*
* @return string
*/
function render(){
$fn = get_class($this);
$ref = new \ReflectionFunction($fn);
$args = [];
foreach($ref->getParameters() as $arg){
$args[] = $this->props->{$arg->getName()} ?? ($arg->isOptional() ? $arg->getDefaultValue() : null);
}
return call_user_func_array($fn, $args);
}
/**
* Get State
*
* @return object
*/
function getState(array $default = []){
$this->state = (object)array_merge($default, (array)$this->state);
return $this->state;
}
}
/**
* Get State in functional component
*
* @return object
*/
function useState(array $default = []){
$trace = debug_backtrace();
$reactFunc = @$trace[3];
if(!$reactFunc || !($reactFunc['object'] instanceof Func)) trigger_error("useState must be called inside functional component");
return $reactFunc['object']->getState($default);
}