初見PHP反射

什麼是反射?指在PHP運行狀態中,擴展分析PHP程序,導出或提取出關於這個類的方法、屬性、參數、註釋等信息。通俗來說就是獲取該類的信息,至於你要獲取來幹嘛,這個就看你自己的需求了。下面是一個小例子:
class Test{
	public $name;
	public $age;

	public static function show(){
		echo 'this is show action';
	}

	public function del(){
		echo $this->name;
	}
	public function __set($name,$value){
		$this->$name = $value;
	}

	public function __get($name){
		if(isset($thsi->$name)){
				return $this->$name;
		}else{
			echo 'error';
		}
	}
}

$obj = new Test();
$obj->name = 'lu';
$obj->age  = 5;

//Reflection 反射類
//以對象作爲反射的對象
$res = new ReflectionObject($obj);
//以類作爲反射的對象	兩個效果相同
//$res = new ReflectionClass('Test');
//獲取$obj對象的一組屬性,返回一個數組 裏面包含 屬性名 和 所屬類
$p = $res->getProperties();
//var_dump($p);
$m = $res->getMethods();
//var_dump($m);


array(2) {
  [0]=>
  &object(ReflectionProperty)#2 (2) {
    ["name"]=>
    string(4) "name"
    ["class"]=>
    string(4) "Test"
  }
  [1]=>
  &object(ReflectionProperty)#3 (2) {
    ["name"]=>
    string(3) "age"
    ["class"]=>
    string(4) "Test"
  }
}


array(4) { [0]=> &object(ReflectionMethod)#5 (2) { ["name"]=> string(4) "show" ["class"]=> string(4) "Test" } [1]=> &object(ReflectionMethod)#6 (2) { ["name"]=> string(3) "del" ["class"]=> string(4) "Test" } [2]=> &object(ReflectionMethod)#7 (2) { ["name"]=> string(5) "__set" ["class"]=> string(4) "Test" } [3]=> &object(ReflectionMethod)#8 (2) { ["name"]=> string(5) "__get" ["class"]=> string(4) "Test" }}



甚至還可以獲取該類的原型:

$obj = new ReflectionClass('Test');
//獲取類名 
$class_name = $obj->getName();
$m = array();	
$p = array();

//獲取該類的屬性 返回一個數組對象 我們把它遍歷出來 
foreach($obj->getProperties() as $v){
	//$V 是一個 ReflectionProperty該類報告了類的屬性的相關信息。 
	$p[$v->getName()] = $v;
}
/*
array(2) {
  ["name"]=>
  object(ReflectionProperty)#2 (2) {
    ["name"]=>
    string(4) "name"
    ["class"]=>
    string(4) "Test"
  }
  ["age"]=>
  object(ReflectionProperty)#3 (2) {
    ["name"]=>
    string(3) "age"
    ["class"]=>
    string(4) "Test"
  }
}
*/

//獲取方法
foreach($obj->getmethods() as $v){
	//$V 是一個 ReflectionMethod類報告了一個方法的有關信息。
	$m[$v->getName()] = $v;
}

/*
array(4) {
  ["show"]=>
  object(ReflectionMethod)#4 (2) {
    ["name"]=>
    string(4) "show"
    ["class"]=>
    string(4) "Test"
  }
  ["del"]=>
  object(ReflectionMethod)#5 (2) {
    ["name"]=>
    string(3) "del"
    ["class"]=>
    string(4) "Test"
  }
  ["__set"]=>
  object(ReflectionMethod)#6 (2) {
    ["name"]=>
    string(5) "__set"
    ["class"]=>
    string(4) "Test"
  }
  ["__get"]=>
  object(ReflectionMethod)#7 (2) {
    ["name"]=>
    string(5) "__get"
    ["class"]=>
    string(4) "Test"
  }
}
*/

//拼湊字符串 組成類的形式
echo "class {$class_name}\n{\n";
//ksort 對數組按照鍵名逆向排序 
is_array($p)&&ksort($p);

foreach($p as $k => $v){
	//此時 $v 還是一個 ReflectionProperty 的一個對象
	echo "\t";
	echo $v->isPublic() ? 'public' : '';
	echo $v->isPrivate() ? 'private' : '';
	echo $v->isProtected() ? 'protected' : '';
	echo "\t{$k}\n";
}

echo "\n";

is_array($m)&&ksort($m);
foreach($m as $k => $v){
	//此時 $v 還是一個 ReflectionProperty 的一個對象
	echo "\tfunction {$k}(){}\n";
}
echo "}\n";

效果:
class Test
{
	public	age
	public	name

	function __get(){}
	function __set(){}
	function del(){}
	function show(){}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章