PHP Implementation Reflection

Source: Internet
Author: User
Define a human

Class  person{public $name;p ublic $gender;p ublic function Say () {//echo $this->name. Say ". $this->gender;} Public Function __set ($name, $value) {$this->name= $value;} Public Function __get ($name) {if (!isset ($name)) {echo "not set";} Else{return $this $name;}}} $student =new person (), $student->name= "Cat", $student->gender= "male"; $student->say (); $student->age = " 12 ";

Get the class name in PHP by reflection, method name

Use the class function Var_dump (Get_object_vars ($student));  Returns an associative array of objects var_dump (Get_class_vars (Get_class ($student)));//properties of the Class Var_dump (Get_class_methods (Get_class ($student)) );//array of method names echo Get_class ($student);//class name

Use reflection Api$reflect = new Reflectionobject ($student), $props = $reflect->getproperties (); foreach ($props as $prop) { Print $prop->getname (). "
";} Returns all methods of an object $m = $reflect->getmethods (), foreach ($m as $prop) {print $prop->getname ();} Var_dump ($props);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.