php反射

<?php

header("Content-type:text/html;charset=utf-8");

class family {

    public $name;
    public $age;

    public function __construct() {
        echo "父类构造函数<br>";
    }

    public function say() {
        echo "父类" . $this->name . "今年," . $this->age;
    }

}

class sun extends family {

    public $name; //与父类属性同名将覆盖父类的name
    public $age;

    public function __construct() {
        parent::__construct();
        echo "子类构造函数<br>";
    }

    public function sayddd() {
        $this->name = "ddddd";
        $this->age = 222;
        $this->say();
    }

}

$sun = new sun();  
$reflect=new ReflectionClass($sun);

//ReflectionObject 继承了ReflectionClass
$props=$reflect->getProperties(); //获取类属性
foreach ($props as $prop=>$value)
{
    print $value->getName()."\n";
}
$method=$reflect->getMethods(); //获取方法
foreach ($method as $key=>$value)
{
    print $value->getName()."\n";
} 

输出:

父类构造函数
子类构造函数
name age __construct sayddd say

posted @ 2015-12-17 15:11  等待是一生最初的苍老  阅读(242)  评论(0编辑  收藏  举报