-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
81 lines (60 loc) · 1.69 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
require 'Foo.php';
$class = 'PhpTest\Foo';
class App
{
public function build($class)
{
$reflect = new ReflectionClass($class);
if (!$reflect->isInstantiable()) {
// 不能被实例化
throw new Exception('cannot be instantiable');
}
// 可以实例化
$constructor = $reflect->getConstructor();
// 没有构造函数
if (is_null($constructor)){
return new $class;
}
// 有构造函数
// 获取构造函数的参数
$params = $constructor->getParameters();
$dependencies = $this->getDependency($params);
return $reflect->newInstanceArgs($dependencies);
}
public function getDependency($params)
{
$dependencies = [];
foreach ($params as $param){
// 获取class 参数
$dependency = $param->getClass();
if (is_null($dependency))
{
// 处理费class类型的依赖
$dependencies[] = $this->resolveNullClass($param);
}else {
// 处理class类型的依赖
$dependencies[] = $this->build($dependency->name);
}
}
return $dependencies;
}
/**
* @param $param
* @return mixed
* @throws Exception
*/
public function resolveNullClass($param)
{
// 有没有默认值
if($param->isDefaultValueAvailable()){
// 有 返回
return $param->getDefaultValue();
}
// 无 异常
throw new Exception('no default value');
}
}
$app = new App();
$object = $app->build($class);
var_dump($object);