Home  >  Article  >  Backend Development  >  Reflection usage examples of PHP classes, php usage examples_PHP tutorial

Reflection usage examples of PHP classes, php usage examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:231041browse

Reflection usage example of PHP class, php usage example

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:

Copy code The code is as follows:
foreach($this->chs as $ch) {

$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.

How to get the type of an object in php

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();

Usage of classes in php

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904927.htmlTechArticle Examples of reflection usage of PHP classes, php usage examples This article describes the reflection usage of PHP classes. Share it with everyone for your reference. The specific implementation method is as follows: This example implements for each channel...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn