我是靠谱客的博主 开心火车,这篇文章主要介绍PHP基于反射获取一个类中所有的方法,现在分享给大家,希望可以做个参考。

本文实例讲述了PHP基于反射获取一个类中所有的方法。分享给大家供大家参考,具体如下:

当我们使用一个类时既没有源码也没有文档时(尤其是php扩展提供的类,比如mysqli,Redis类),我们该怎么知道这个类中提供了哪些方法,以及每个方法该怎么使用呢,此时就该PHP中强大的反射登场了,下面以Redis扩展为例用代码演示:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php $ref = new ReflectionClass('Redis'); $consts = $ref->getConstants(); //返回所有常量名和值 echo "----------------consts:---------------" . PHP_EOL; foreach ($consts as $key => $val) { echo "$key : $val" . PHP_EOL; } $props = $ref->getDefaultProperties(); //返回类中所有属性 echo "--------------------props:--------------" . PHP_EOL . PHP_EOL; foreach ($props as $key => $val) { echo "$key : $val" . PHP_EOL; // 属性名和属性值 } $methods = $ref->getMethods(); //返回类中所有方法 echo "-----------------methods:---------------" . PHP_EOL . PHP_EOL; foreach ($methods as $method) { echo $method->getName() . PHP_EOL; }

返回结果:

复制代码
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
----------------consts:--------------- REDIS_NOT_FOUND : 0 REDIS_STRING : 1 REDIS_SET : 2 REDIS_LIST : 3 REDIS_ZSET : 4 REDIS_HASH : 5 ATOMIC : 0 MULTI : 1 PIPELINE : 2 OPT_SERIALIZER : 1 OPT_PREFIX : 2 OPT_READ_TIMEOUT : 3 SERIALIZER_NONE : 0 SERIALIZER_PHP : 1 OPT_SCAN : 4 SCAN_RETRY : 1 SCAN_NORETRY : 0 AFTER : after BEFORE : before --------------------props:-------------- -----------------methods:--------------- __construct __destruct connect pconnect close ping echo get set setex psetex setnx getSet randomKey renameKey renameNx getMultiple exists delete incr incrBy incrByFloat decr decrBy type append getRange setRange getBit setBit strlen getKeys sort sortAsc sortAscAlpha sortDesc sortDescAlpha lPush rPush lPushx rPushx lPop rPop blPop brPop lSize lRemove listTrim lGet lGetRange lSet lInsert sAdd sSize sRemove sMove sPop sRandMember sContains sMembers sInter sInterStore sUnion sUnionStore sDiff sDiffStore setTimeout save bgSave lastSave flushDB flushAll dbSize auth ttl pttl persist info resetStat select move bgrewriteaof slaveof object bitop bitcount bitpos mset msetnx rpoplpush brpoplpush zAdd zDelete zRange zReverseRange zRangeByScore zRevRangeByScore zRangeByLex zCount zDeleteRangeByScore zDeleteRangeByRank zCard zScore zRank zRevRank zInter zUnion zIncrBy expireAt pexpire pexpireAt hGet hSet hSetNx hDel hLen hKeys hVals hGetAll hExists hIncrBy hIncrByFloat hMset hMget multi discard exec pipeline watch unwatch publish subscribe psubscribe unsubscribe punsubscribe time eval evalsha script debug dump restore migrate getLastError clearLastError _prefix _serialize _unserialize client scan hscan zscan sscan pfadd pfcount pfmerge getOption setOption config slowlog rawCommand getHost getPort getDBNum getTimeout getReadTimeout getPersistentID getAuth isConnected getMode wait pubsub open popen lLen sGetMembers mget expire zunionstore zinterstore zRemove zRem zRemoveRangeByScore zRemRangeByScore zRemRangeByRank zSize substr rename del keys lrem ltrim lindex lrange scard srem sismember zrevrange sendEcho evaluate evaluateSha

进一步当我们想要知道具体一个方法怎么使用,有哪些参数时,我们可以对这个方法进行进一步的反射,以上例中的bitpos方法为例(文档中并没有介绍该方法的使用)

复制代码
1
2
3
echo '---------------------params-----------------------' . PHP_EOL . PHP_EOL; $reflectMethod = $ref->getMethod('bitpos'); //传入方法名即可 echo $reflectMethod; // 会调用$reflectMethod->__toString() 返回可打印的形式;

打印结果:

复制代码
1
2
3
---------------------params----------------------- Method [ <internal:redis> public method bitpos ] { }

并没有看到需要参数,可能与该方法的具体实现有关,具体原因只能去看redis扩展的代码实现,正常情况下应该是返回如下的形式,以mysqli的select_db方法为例:

复制代码
1
2
3
4
$ref = new ReflectionClass('mysqli'); echo '---------------------params-----------------------' . PHP_EOL . PHP_EOL; $reflectMethod = $ref->getMethod('select_db'); //传入方法名即可 echo $reflectMethod; // 会调用$reflectMethod->__toString() 返回可打印的形式;
复制代码
1
2
3
4
5
6
---------------------params----------------------- Method [ <internal:mysqli> public method select_db ] { - Parameters [1] { Parameter #0 [ <required> $database ] } }

这时就没办法了 我们只能靠对redis的理解以及参考相似方法来使用了,比如bitop

复制代码
1
public function bitOp( $operation, $retKey, ...$keys) {}

贴下最终的方法调用

复制代码
1
2
3
4
5
$redis = new Redis(); $redis->connect('127.0.0.1'); $redis->setBit('bit', 15, 1); echo 'bitpos: ' . $redis->bitpos('bit', 1) . PHP_EOL; //bitpos: 15 $redis->close();

是不是很有趣呢!

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

最后

以上就是开心火车最近收集整理的关于PHP基于反射获取一个类中所有的方法的全部内容,更多相关PHP基于反射获取一个类中所有内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(127)

评论列表共有 0 条评论

立即
投稿
返回
顶部