forked from Hi-starlet/my_program
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflection.php
More file actions
77 lines (66 loc) · 1.58 KB
/
reflection.php
File metadata and controls
77 lines (66 loc) · 1.58 KB
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
73
74
75
76
77
<?php
class Person
{
/**
** For the sake of demonstration, we"re setting this private
**/
private $_allowDynamicAttributes = false;
/** type=primary_autoincrement */
protected $id = 0;
/** type=varchar length=255 null */
protected $name;
/** type=text null */
protected $biography;
public $ex = 12;
public function getId()
{
return $this->id;
}
public function setId($v)
{
$this->id = $v;
}
public function getName()
{
return $this->name;
}
public function setName($v)
{
$this->name = $v;
}
public function getBiography()
{
return $this->biography;
}
public function setBiography($v)
{
$this->biography = $v;
}
}
$class = new ReflectionClass('Person');//建立 Person这个类的反射类
$obj = $class->newInstanceArgs(); //相当于实例化Person类
//给属性赋值
$properties = $class->getProperties();
$b = $properties[4]->name;
$obj->$b = 13;
var_dump($obj->$b);
echo "\n";
//执行类的方法
$obj->setBiography(22);
echo $obj->getBiography(); //执行Person里面的方法getBiography
//或者
//获得所有方法
$ec = $class->getMethods();
//获得单个方法
$ec = $class->getMethod('setName');
$ec->invoke($obj,'xlc');
echo "\n".$obj->getName();
exit;
print_r($properties);exit;
foreach($properties as $property) {
echo $property->getName()."\n";
}
//var_dump($class);
/*
$instance = $class->newInstanceArgs($args);//相当于实例化Person 类
*/