PHP Reflection (Reflection) Use instance _php tips

Source: Internet
Author: User
Tags export class php class

The

PHP reflection is used to get details about the classes, extensions, methods, functions, objects, parameters, and properties. The
Reflectionclass class obtains class-related information, such as getting properties, methods, document comments, and so on.

<?php class Person {/** * For the sake of demonstration, we ' re setting this private * * Private $_allowdyn
 
  Amicattributes = false;
 
  /** type=primary_autoincrement * * protected $id = 0;
 
  /** Type=varchar length=255 NULL * * * protected $name;
 
  /** type=text NULL */protected $biography;
  Public Function GetId () {return $this->id;
  The Public Function setId ($v) {$this->id = $v;
  The Public Function GetName () {return $this->name;
  The Public Function SetName ($v) {$this->name = $v;
  The Public Function getbiography () {return $this->biography;
  The Public Function setbiography ($v) {$this->biography = $v;
 
}///Export class Reflectionclass::export (' person ');
 
$r = new Reflectionclass (' person ');
 
Gets all Properties Print_r ($r->getproperties ()); /** * Get the specified property * reflectionproperty::is_static * reflectionproperty::is_public * reflectionproperty::is_protected * REF Lectionproperty::is_private * * PRINT_R ($r->getproperties (reflectionproperty::is_private));
 
Gets the annotation print_r ($r->getproperty (' id ')->getdoccomment ());
 Get method Print_r ($r->getmethods ());

The Reflectionextension class is used to obtain extension-related information

$re = new Reflectionextension (' Reflection ');
Print_r ($re->getclasses ()); Extended All Class
Print_r ($re->getclassnames ());//Extend all class names
 
$dom = new Reflectionextension (' MySQL ');
Print_r ($dom->getconstants ())//Extended constant
Print_r ($dom->getdependencies ());//This extension relies on
Print_r ($dom-> Getfunctions ())//extension method
Print_r ($dom->getinientries ());//Extended INI Information
print_r ($dom->getname ());//Extended Name
Print_r ($dom->getversion ())//Extended version
Print_r ($dom->info ())//Extended information
print_r ($dom-> Ispersistent ());/Whether it is a persistent extension
Print_r ($dom->istemporary ());//Whether it is a temporary extension

Reflectionfunction class User Gets the information about the function

$RF = new Reflectionfunction (' Array_merge ');
 
foreach ($rf->getparameters () as $item) {
  echo $item. Php_eol;
}

Reflectionmethod-Class user access methods related information

Class Person {public
 
  $name;
 
  /** * Get name of person *
   /public
  function GetName ()
  {return
    $this->name;
  }
  Public Function SetName ($v)
  {
    $this->name = $v;
  }
}
 
$RM = new Reflectionmethod (' person ', ' getName ');
 
Print_r ($rm->ispublic ());
Print_r ($rm->getdoccomment ());

Reflectionobject class is used to get information about objects

Class Person {public
 
  $name;
 
  Public function __construct ($name)
  {
    $this->name = $name;
  }
  
  Public Function GetName ()
  {return
    $this->name;
  }
  
  Public Function SetName ($v)
  {
    $this->name = $v;
  }
}
 
$a = new person (' a ');
 
$ro = new Reflectionobject ($a);
 
Print_r ($ro->getmethods ());

Reflectionparameter gets information about a function or method parameter.

Class Person {public
 
  $name;
 
  Public function __construct ($name)
  {
    $this->name = $name;
  }
 
  Public Function GetName ()
  {return
    $this->name;
  }
 
  Public Function SetName ($v)
  {
    $this->name = $v;
  }
}
 
$p = new Reflectionparameter (Array (' person ', ' setname '), 0);
 
Print_r ($p->getposition ()); 0
Print_r ($p->getname ());//v

Reflectionproperty gets information about the properties of the class.

Class Person {
 
  /** Test
  /public $name;
 
  Public function __construct ($name)
  {
    $this->name = $name;
  }
 
  Public Function GetName ()
  {return
    $this->name;
  }
 
  Public Function SetName ($v)
  {
    $this->name = $v;
  }
}
 
$p = new Reflectionproperty (' person ', ' name ');
 
Print_r ($p->getdoccomment ());

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.