PHP reflection dynamic access class method, attribute, parameter operation example

Time:2021-5-1

This article describes the PHP reflection dynamic access class method, attribute, parameter operation. The details are as follows:

We can dynamically obtain the detailed information of class methods, properties, parameters and so on through PHP reflection when PHP is running.

Purpose: Design of plug-in, automatic generation of documents, expansion of PHP language.

<?php
class Person {
  const weightUnit = 'kg';
  const heightUnit = 'cm';
  public $name = 'test';
  public $age = 1;
  public function say($msg = '') {
    echo $msg;
  }
}

$p = new Person ();
//Ordinary instantiation object, call method
$p->say ( 'hello' );
echo "<br/>";
//Create a reflection class for person
$rp = new ReflectionClass ( 'Person' );

//Get the detailed information of the class through the method of reflectionclass

//Get constant
echo $rp->getConstant ( 'weightUnit' );
echo "<br/>";
//Gets the constant defined in the class
var_dump ( $rp->getConstants () );

//Get the property and return a reflectionproperty class
$propName = $rp->getProperty ( 'name' );
echo $propName->getName(), ':', $propName->getValue ( new Person () );
echo "<br/>";
//Gets a set of properties defined in a class
$propArr = $rp->getProperties ();
foreach ( $propArr as $obj ) {
  echo $obj->getName (), ':', $obj->getValue ( new Person () );
}
echo "<br/>";
//Get the method and return a ReflectionMethod class
$sayMetd = $rp->getMethod('say');
if($sayMetd->isPublic() && !$sayMetd->isAbstract()) {
  $sayMetd->invoke(new Person(), 'hehe');
  $sayMetd->invokeArgs(new Person(), array('hehe'));
}

//Gets a set of methods defined in a class to filter unnecessary methods
$metds = $rp->getMethods();

//Get namespace
echo $rp->getNamespaceName();
echo "<br/>";
//Determine whether a method is defined
if($rp->hasMethod('say')) {
  echo 'say has';
}
echo "<br/>";
//Determine whether an attribute is defined
if($rp->hasProperty('name')) {
  echo 'name has';
}

Results of operation:

hello
kg
array(2) { [“weightUnit”]=> string(2) “kg” [“heightUnit”]=> string(2) “cm” } name:test
name:testage:1
hehehehe
say has
name has

For more information about PHP, interested readers can see the following topics: PHP object-oriented programming introductory course, PHP array operation skills encyclopedia, PHP basic grammar introductory course, PHP operation and operator usage summary, PHP string Usage Summary “PHP + MySQL database operation tutorial” and “PHP common database operation skills summary”

I hope this article is helpful for PHP programming.

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]