类反射reflectionclass


http://php.net/manual/en/class.reflectionclass.php';
echo 'aaa';
echo '';
echo "xxxx";

//***************************ReflectionClass类 和 ReflectionMethod类
class Worker {
    public $name='工人';
    protected $age;
    private $money;
    public function __construct($a) {
        echo '构造方法取得参数为:'.$a.'
'; } public function greeting() { echo 'Hi there, this is'.$this->name.'
'; } protected function gotowork(){ echo 'Go to company to work'; } protected function drive(){ echo 'drive to work'; } } class Teacher { public $name='老师'; protected $age; private $money; private $height; private $weight; public function __construct($a, $b) { echo '构造方法取得参数为:'.$a.$b.'
'; $this->a = $a; $this->b = $b; } public function greeting() { echo 'Hi there, I am a teacher from High school, and my name is'.$this->name.'
'; echo 'Construct args $this->a=', $this->a, ', $this->b=', $this->b.'
'; } protected function gotowork(){ echo 'Go to school to work'; } protected function drive(){ echo 'drive to school to work'; } } echo '类反射测试
获取类的属性:
'; $class = new ReflectionClass('teacher'); $properties = $class->getProperties(); foreach ($properties as $property) { echo $property->getName() . "
"; } echo '
====getEndLine====
'; echo $class->getEndLine(); echo '
====getExtensionName====
'; echo $class->getExtensionName(); echo '
====getFileName====
'; echo $class->getFileName(); echo '
====getMethods====
'; echo "
";
print_r($class->getMethods()) ;
echo "
"; echo '
====getModifiers====
'; echo $class->getModifiers(); echo '
========
'; class Instance { function __call($className, $arguments) { echo "

调用本类没有的方法时显示此消息:

"; echo "
返回类:==".$className; echo "
返回参数==:"; if(is_array($arguments)){ var_dump($arguments); echo "
"; }else{ echo $arguments;echo "
"; } $class = new ReflectionClass($className); return $class->newInstanceArgs($arguments); } public function callname() { echo "

调用到了INSTANCE的方法

"; } } $inst = new INSTANCE(); $a1='worker'; $a2='teacher'; $foo = $inst->$a2(1,2,3,4,5); $foo = $inst->teacher(1,2,3,4,5); $foo->greeting();

你可能感兴趣的:(类反射reflectionclass)