开发者

Can I use PHP reflection to extract function code?

开发者 https://www.devze.com 2023-01-27 13:16 出处:网络
I am using PHP ReflectionClass to extract as开发者_StackOverflow中文版 much information about a class as possible. Can I also use this to get return values from functions? Or does that not make sense

I am using PHP ReflectionClass to extract as开发者_StackOverflow中文版 much information about a class as possible. Can I also use this to get return values from functions? Or does that not make sense since reflection only profiles what an object accepts?


You can not rely, if a function has a well defined return value you can simply extract from the source code. Imagine something like this:

return $this->isValid() ? $result : $this->createNullObject();

Thats hard (/impossible) to parse just to get the return value. You can use DocComments instead. @return is the usual tag for that use

/**
 * MyMethod
 *
 * @return int
 */

Call getDocComment() on a ReflectionMethod-object and then parse the docComment.


for internal functions you could use

$reflect = new ReflectionExtension('standard');
echo "<pre>" . $reflect . "</pre>"; 
0

精彩评论

暂无评论...
验证码 换一张
取 消