面向對象編輯中對象被賦予了自省的能力,而這個自省的過程就是反射.php
class HandsonBoy { public $name = 'chenqionghe'; public $age = 18; public function __set($name,$value) { echo '您正在設置私有屬性'.$name.'<br >值爲'.$value.'<br>'; $this->$name = $value; } public function __get($name) { if(!isset($this->$name)) { echo '未設置'.$name; $this->$name = "正在爲你設置默認值".'<br>'; } return $this->$name; } } $boy = new HandsonBoy(); echo $boy->name.'<br />'; $boy->hair = 'short';
$reflect = new ReflectionObject($boy); $props = $reflect->getProperties(); //獲取屬性的名字 foreach($props as $prop) { print $prop->getName().PHP_EOL; } //獲取對象方法列表 $methos = $reflect->getMethods(); foreach($methos as $method) { print $method->getName().PHP_EOL; }
也能夠不用反射API,使用class函數,返回對象屬性的關聯數組以及更多的信息:(針對於公開的屬性和):mysql
//返回對象屬性的關聯數組 var_dump(get_object_vars($boy)); //類屬性 var_dump(get_class_vars(get_class($boy))); //返回由類的屬性的方法名組成的數組 var_dump(get_class_methods(get_class($boy)));
反射API的功能顯然更強大,甚至能還原這個類的原型,包括方法的訪問權限,如下簡單封裝了一個打印類的代碼sql
/** * @param $classObject 對象或者類名 */ function getClass($classObject) { $object = new ReflectionClass($classObject); $className = $object->getName(); $Methods = $Properties = array(); foreach($object->getProperties() as $v) { $Properties[$v->getName()] = $v; } foreach($object->getMethods() as $v) { $Methods[$v->getName()] = $v; } echo "class {$className}\n{\n"; is_array($Properties) && ksort($Properties); foreach($Properties as $k=>$v) { echo "\t"; echo $v->isPublic() ? 'public' : '',$v->isPrivate() ? 'private' :'',$v->isProtected() ? 'protected' : ''; $v->isStatic() ? 'static' : ''; echo "\t{$k}\n"; } echo "\n"; if(is_array($Methods)) ksort($Methods); foreach($Methods as $k=>$v) { echo "\tfunction {$k}()\n"; } echo "}\n"; }
不只如此,PHP手冊中關於反射API更是有幾十個,能夠說,反射完整地描述了一個類或者對象的原型.反射不只能夠用於類和對象,還能夠用於函數,擴展模塊,異常等.數據庫
<?php class mysql { function connect($db) { echo "鏈接到數據庫{$db[0]}".PHP_EOL; } } class sqlproxy { private $target; public function __construct($tar) { $this->target[] = new $tar; } public function __call($name,$args) { foreach($this->target as $obj) { $r = new ReflectionClass($obj); if($method = $r->getMethod($name)) { if($method->isPublic() && !$method->isAbstract()) { echo "方法前攔截記錄LOG".PHP_EOL; $method->invoke($obj,$args); echo "方法後攔截".PHP_EOL; } } } } } $obj = new sqlproxy('mysql'); $obj->connect('chenqionghe');