say ( 'hello' ); echo "
"; // 创建一个Person的反射类 $rp = new ReflectionClass ( 'Person' ); // 通过ReflectionClass的方法来获取类的详细信息 // 获取常量 echo $rp->getConstant ( 'weightUnit' ); echo "
"; // 获取类中已定义的常量 var_dump ( $rp->getConstants () ); // 获取属性,返回的是一个ReflectionProperty类 $propName = $rp->getProperty ( 'name' ); echo $propName->getName(), ':', $propName->getValue ( new Person () ); echo "
"; // 获取类中已定义的一组属性 $propArr = $rp->getProperties (); foreach ( $propArr as $obj ) { echo $obj->getName (), ':', $obj->getValue ( new Person () ); } echo "
"; //获取方法,返回的是一个ReflectionMethod类 $sayMetd = $rp->getMethod('say'); if($sayMetd->isPublic() && !$sayMetd->isAbstract()) { $sayMetd->invoke(new Person(), 'hehe'); $sayMetd->invokeArgs(new Person(), array('hehe')); } //获取类中已定义的一组方法,可以过滤不需要的方法 $metds = $rp->getMethods(); //获取命名空间 echo $rp->getNamespaceName(); echo "
"; //判断一个方法是否定义 if($rp->hasMethod('say')) { echo 'say has'; } echo "
"; //判断一个属性是否定义 if($rp->hasProperty('name')) { echo 'name has'; }
运行结果:
hello
kg
array(2) { ["weightUnit"]=> string(2) "kg" ["heightUnit"]=> string(2) "cm" } name:test
name:testage:1
hehehehe
say has
name has
上一篇:PHP中有哪些函数参数传递的方式