@nguyenvanduocit/

reflectionFunction

PHP (Legacy)

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.44115182627095817","path":"main.php","file":{"path":"main.php","content":{"asEncoding":{"base64":"LyoqCiAqIERlZmluZQoqKi8KCmNsYXNzIFVzZXJ7CiAgcHJpdmF0ZSAkbmFtZTsKICBwcml2YXRlICRpZDsKICBwdWJsaWMgZnVuY3Rpb24gX19jb25zdHJ1Y3QoJGlkLCAkbmFtZSl7CiAgICAkdGhpcy0+aWQgPSAkaWQ7CiAgICAkdGhpcy0+bmFtZSA9ICRuYW1lOwogIH0KICBwdWJsaWMgZnVuY3Rpb24gZ2V0TmFtZSAoKSB7CiAgICByZXR1cm4gJHRoaXMtPm5hbWU7CiAgfQp9CgpmdW5jdGlvbiBnZXRVc2VyTmFtZShVc2VyICR1c2VyKXsKICByZXR1cm4gJHVzZXItPmdldE5hbWUoKTsKfQoKCmZ1bmN0aW9uIGV4Y3V0ZUZ1bmN0aW9uKCRmdW5jdGlvbk5hbWUpewogICRyZWZsZWN0b3IgPSBuZXcgUmVmbGVjdGlvbkZ1bmN0aW9uKCRmdW5jdGlvbk5hbWUpOwogICRwYXJhbXMgPSBbXTsKICBmb3JlYWNoKCAkcmVmbGVjdG9yLT5nZXRQYXJhbWV0ZXJzKCkgYXMgJHBhcmFtICl7CiAgICBzd2l0Y2ggKCRwYXJhbS0+Z2V0Q2xhc3MoKS0+Z2V0TmFtZSgpKXsKICAgICAgY2FzZSAnVXNlcic6CiAgICAgICAgZ2xvYmFsICR1c2VyOwogICAgICAgICRwYXJhbXNbXSA9ICR1c2VyOwogICAgICAgIGJyZWFrOwogICAgICBkZWZhdWx0OgogICAgICAgICRwYXJhbXNbXSA9IG51bGw7CiAgICAgICAgYnJlYWs7CiAgICB9CiAgfQogIHJldHVybiAkcmVmbGVjdG9yLT5pbnZva2VBcmdzKCRwYXJhbXMpOwp9CgovKioKICogVXNhZ2UKKiovCmdsb2JhbCAkdXNlcjsKJHVzZXIgPSBuZXcgVXNlcigxLCAiRHVvYyIpOwoKJGN1cnJlbnRVc2VyTmFtZSA9IGV4Y3V0ZUZ1bmN0aW9uKCdnZXRVc2VyTmFtZScpOwp2YXJfZHVtcCgkY3VycmVudFVzZXJOYW1lKTs="},"asBuffer":null},"loaded":true}}
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
/**
 * Define
**/

class User{
  private $name;
  private $id;
  public function __construct($id, $name){
    $this->id = $id;
    $this->name = $name;
  }
  public function getName () {
    return $this->name;
  }
}

function getUserName(User $user){
  return $user->getName();
}


function excuteFunction($functionName){
  $reflector = new ReflectionFunction($functionName);
  $params = [];
  foreach( $reflector->getParameters() as $param ){
    switch ($param->getClass()->getName()){
      case 'User':
        global $user;
        $params[] = $user;
        break;
      default:
        $params[] = null;
        break;
    }
  }
  return $reflector->invokeArgs($params);
}

/**
 * Usage
**/
global $user;
$user = new User(1, "Duoc");

$currentUserName = excuteFunction('getUserName');
var_dump($currentUserName);
PHP 7.0.8 (Legacy: use the following for new features: Command-line PHP: https://repl.it/languages/php_cli PHP Web Server: https://repl.it/languages/php7