Home > Article > Backend Development > Reflection usage examples of PHP classes, php usage examples_PHP tutorial
The examples in this article describe the use of reflection in PHP classes. Share it with everyone for your reference. The specific implementation method is as follows:
This example implements obtaining the corresponding class for each channel to perform the corresponding operation. The details are as follows:
$className = $this->chsMap[$ch];
If($className) { // If it is a legal class name
// Get reflection class
$class = new ReflectionClass($className);
// Get the class method
$recd = $class->getmethod('exeRecd');
// Generate an instance of the class
$instance = $class->newInstance($this->qq, $this->ip);
//Execution method
$result[$ch] = $recd->invoke($instance);
}
}
I hope this article will be helpful to everyone’s PHP programming design.
To get the type of an object, use the gettype() function:
to get which object it is For instances of classes, use the get_class() function:
to get the methods and attributes of a class or object, To use reflection:
getProperties(); // will return a Group object, please refer to the PHP manual for usage $ref->getMethods(); // A group of objects will be returned, please refer to the PHP manual for usage $obj = new MyClass();$ref = new ReflectionObject($obj);$ref-> ;getProperties();$ref->getMethods();
class myclass
{
function showerror($msg)
{
$msg=trim($msg);
echo "";
exit;
}
}
?>
This is a class I wrote that prompts errors and returns the history page. The advantage of using classes is that it saves time on repeatedly writing code, and also saves a lot of time on secondary development of programming.
Before using classes, To create an instance of a class
$lei=new myclass(); this instantiates
and then use
$lei->showerror($mess); so that you can call functions and members in the class Variable