100分求一个比较少见的问题

最帅马老师 2010-06-02 04:53:55
现在有一个PHP类,如何在不知其内部构造的情况下,遍历打印出其内部的所有函数名?
...全文
166 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
tbraz 2010-06-03
  • 打赏
  • 举报
回复
正在学习中
ms_X0828 2010-06-03
  • 打赏
  • 举报
回复
下面为我的一个类:
class ParserXml
{
private $config=array();

public function ParserXml()
{}

public function parserXml2($filename)
{}


private function XmlToArray($config,$DomDocument)
{}

public function aaa($filename){
}

private function object2Array($object)
{}
}


下面为我的test类:

include 'parse.php';

$p=new ParserXml();
print_r(get_class_methods(get_class($p)));

echo "<br/><br/>";

$reflection = new ReflectionClass(get_class($p));

print_r($reflection->getMethods());



输出的结果如下:
Array
(
[0] => ParserXml
[1] => parserXml2
[2] => aaa
)
<br/><br/>Array
(
[0] => ReflectionMethod Object
(
[name] => ParserXml
[class] => ParserXml
)

[1] => ReflectionMethod Object
(
[name] => parserXml2
[class] => ParserXml
)

[2] => ReflectionMethod Object
(
[name] => XmlToArray
[class] => ParserXml
)

[3] => ReflectionMethod Object
(
[name] => aaa
[class] => ParserXml
)

[4] => ReflectionMethod Object
(
[name] => object2Array
[class] => ParserXml
)

)

get_class_methods()这个不能得到私有的方法

huashuowoaini 2010-06-03
  • 打赏
  • 举报
回复
学习中
zilingzhi 2010-06-03
  • 打赏
  • 举报
回复
函数名是不是都有FUNCTION?

你就指定程序把FUNCTION后面那个单词+括号提出来就是了
joogoo 2010-06-03
  • 打赏
  • 举报
回复
mark
地雷 2010-06-02
  • 打赏
  • 举报
回复
学习~
foolbirdflyfirst 2010-06-02
  • 打赏
  • 举报
回复
php5的话就用反射机制咯。。

Reflection
Introduction
PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions and methods as well as extensions. Additionally, the reflection API also offers ways of retrieving doc comments for functions, classes and methods.

例子 19-31. Basic usage of the reflection API

<?php
Reflection::export(new ReflectionClass('Exception'));
?>

上例将输出:

Class [ <internal> class Exception ] {

- Constants [0] {
}

- Static properties [0] {
}

- Static methods [0] {
}

- Properties [6] {
Property [ <default> protected $message ]
Property [ <default> private $string ]
Property [ <default> protected $code ]
Property [ <default> protected $file ]
Property [ <default> protected $line ]
Property [ <default> private $trace ]
}

- Methods [9] {
Method [ <internal> final private method __clone ] {
}

Method [ <internal> <ctor> public method __construct ] {

- Parameters [2] {
Parameter #0 [ <required> $message ]
Parameter #1 [ <required> $code ]
}
}

Method [ <internal> final public method getMessage ] {
}

Method [ <internal> final public method getCode ] {
}

Method [ <internal> final public method getFile ] {
}

Method [ <internal> final public method getLine ] {
}

Method [ <internal> final public method getTrace ] {
}

Method [ <internal> final public method getTraceAsString ] {
}

Method [ <internal> public method __toString ] {
}
}
}


Abin-2008 2010-06-02
  • 打赏
  • 举报
回复
get_class_methods //返回由类的方法名组成的数组

get_class_vars //返回由类的默认属性组成的数组
xuzuning 2010-06-02
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hguisu 的回复:]
5楼正解!
[/Quote]
不要误人子弟!
freedom_gooboy 2010-06-02
  • 打赏
  • 举报
回复
className 已知类名
$reflection = new ReflectionClass('className');
$aMethods = $reflection->getMethods();
var_dump($aMethods);
hguisu 2010-06-02
  • 打赏
  • 举报
回复
5楼正解!
xuzuning 2010-06-02
  • 打赏
  • 举报
回复
get_class_methods -- 返回由类的方法名组成的数组
可通过 php 反射机制探知方法的相关信息

kyzy_yy_pm 2010-06-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jlzan1314 的回复:]
呵呵
这还少见啊..试试get_class_methods ()

这个只能打印出类的成员函数, 而不是函数
[/Quote]+
itian 2010-06-02
  • 打赏
  • 举报
回复
$c = new Class;
var_dump($c)
itian 2010-06-02
  • 打赏
  • 举报
回复
实例化一个对象

然后var_dump

比如
var $c = new Class;
var_dump($c);
kfjfox 2010-06-02
  • 打赏
  • 举报
回复
学习学习学习
garcon1986 2010-06-02
  • 打赏
  • 举报
回复
不太清楚哦 顶~
jlzan1314 2010-06-02
  • 打赏
  • 举报
回复
呵呵
这还少见啊..试试get_class_methods ()

这个只能打印出类的成员函数, 而不是函数
liuahuilele 2010-06-02
  • 打赏
  • 举报
回复
懂的比较少
其他的不知道 要是知道 文件路径名 可以直接得到此文件 然后用正则 比较傻的办法

21,881

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧