网站制作学习网PHP→正文:php反射API-1
字体:

php反射API-1

PHP 2010/8/13 18:56:39  点击:不统计


反射API是PHP中几种内置的OOP扩展特性之一,它包含一系列的类,异常和接口,一起使用它们将允许你分析其他类,接口,属性,方法,函数和扩展。
php Reflection php的反射api:
class Reflection {}
interface Reflector {}
class ReflectionException extends Exception {}扩展标准异常并由API映射抛出。引入了非特定方法或属
class ReflectionMethod extends ReflectionFunction {}返回对象的方法信息
class ReflectionObject extends ReflectionClass {}返回对象的信息
class ReflectionFunction implements Reflector {}类允许你反向设计函数
class ReflectionParameter implements Reflector {}类取回一个函数或方法的参数的信息
class ReflectionClass implements Reflector {}//返回类信息
class ReflectionProperty implements Reflector {}返回类属性新年系
class ReflectionExtension implements Reflector {}
就php反射API简单举例
<?
Reflection::export(new ReflectionExtension('reflection'));
?>
这样就输出了reflection类所有的扩展的名称,参数,参数个数等信息,内容太多,不再列出
<?php
class a{
 function write($name){
 echo $name;
 }
}
reflection::export(new reflectionclass("a"));//参数为类名
?>
这样输出了这个类的详细信息:Class [ class a ] { @@ E:\APMServ5.2.0\www\htdocs\index1.php 2-6 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [0] { } - Methods [1] { Method [ public method write ] { @@ E:\APMServ5.2.0\www\htdocs\index1.php 3 - 5 - Parameters [1] { Parameter #0 [ $name ] } } } }
<?php
function forasp($cn){
echo $cn;
}
$c = new forasp("forasp");
reflection::export(new ReflectionFunction("forasp"));//第一个,参数为类名
reflection::export(new ReflectionObject($c));//第二个,参数为对象或者实例
?>
这样输出这个函数的详细信息第一个:Function [ function forasp ] { @@ E:\APMServ5.2.0\www\htdocs\index1.php 2 - 4 - Parameters [1] { Parameter #0 [ $cn ] } }
第二个输出:Object of class [ class forasp ] { @@ E:\APMServ5.2.0\www\htdocs\index1.php 2-6 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [0] { } - Dynamic properties [0] { } - Methods [1] { Method [ public method cn ] { @@ E:\APMServ5.2.0\www\htdocs\index1.php 3 - 5 - Parameters [1] { Parameter #0 [ $name ] } } } }
<?php
class forasp{
 private $time;
}
reflection::export(new ReflectionProperty("forasp","time"));//参数第一个为类名,第二个为属性
?>
输出:Property [ private $time ]
<?php
function forasp($time){
echo $time;
}
reflection::export(new ReflectionParameter("forasp","time"));
?>
输出:Parameter #0 [ $time ]
<?php
class forasp{
 function methor($foraspcn){}
 private $time;
}
reflection::export(new ReflectionMethod("forasp","methor"));
?>
输出:Method [ public method methor ] { @@ E:\APMServ5.2.0\www\htdocs\index1.php 3 - 3 - Parameters [1] { Parameter #0 [ $foraspcn ] } }


·上一篇:php反射api >>    ·下一篇:php.ini配置介绍 >>
推荐文章
最新文章