php 实现反射

WBOY
發布: 2016-06-23 13:24:07
原創
999 人瀏覽過

定义一个人类 

class  person{		public $name;	public $gender;	public function say(){		//echo $this->name."say".$this->gender;	}	public function __set($name,$value){		$this->name=$value;	}	public function __get($name){				if(!isset($name)){			echo "未设置";		}else{			return $this->$name;		}	}}$student =new person();$student->name="cat";$student->gender="male";$student->say();$student->age ="12";
登入後複製

通过反射获取php中的类名,方法名

//使用class函数var_dump(get_object_vars($student));  //返回对象的关联数组var_dump(get_class_vars(get_class($student)));// 类的属性var_dump(get_class_methods(get_class($student)));//方法名的数组echo get_class($student);//类名
登入後複製

//使用反射API$reflect = new ReflectionObject($student);$props = $reflect->getProperties();foreach($props as $prop){	print $prop->getName()."</br>";} // 返回对象的所有方法$m = $reflect->getMethods();foreach($m as $prop){	print $prop->getName();}var_dump($props);
登入後複製

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!