main.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
/**
 * 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 (cli) (built: Jun 23 2016 23:39:14) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies