PHP reflection mechanism usage examples, php reflection examples_PHP tutorial

WBOY
Release: 2016-07-13 10:19:57
Original
821 people have browsed it

PHP reflection mechanism usage examples, php reflection examples

The examples in this article describe the use of PHP reflection mechanism and are shared with you for your reference. The specific method is as follows:

The demo sample code is as follows:

<&#63;php
class ClassOne {
  function callClassOne() {
    print "In Class One";
  }
}
class ClassOneDelegator {
  private $targets;
  function __construct() {
    $this->target[] = new ClassOne();
  }
  function __call($name, $args) {
    foreach ($this->target as $obj) {
      $r = new ReflectionClass($obj);
      if ($method = $r->getMethod($name)) {
        if ($method->isPublic() && !$method->isAbstract()) {
          return $method->invoke($obj, $args);
        }
      }
    }
  }
}
$obj = new ClassOneDelegator();
$obj->callClassOne();
&#63;>
Copy after login

Output result:

In Class One

It can be seen that his method is implemented through the proxy class ClassOneDelegator instead of the ClassOne class.

Similarly, the following code can also be run:

<&#63;php
class ClassOne {
  function callClassOne() {
    print "In Class One";
  }
}
class ClassOneDelegator {
  private $targets;
  function addObject($obj) {
    $this->target[] = $obj;
  }
  function __call($name, $args) {
    foreach ($this->target as $obj) {
      $r = new ReflectionClass($obj);
      if ($method = $r->getMethod($name)) {
        if ($method->isPublic() && !$method->isAbstract()) {
          return $method->invoke($obj, $args);
        }
      }
    }
  }
}
$obj = new ClassOneDelegator();
$obj->addObject(new ClassOne());
$obj->callClassOne();
&#63;>

Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

What is PHP’s reflection mechanism

It can also be called mapping. To put it bluntly, it can not only clone objects, but also call variables and even methods of objects, which is quite powerful. PHP API5 has an explanation about objects. You can read it if you have a chance. It is similar to the one in

java. Of course, this feature is enough to prove that there is a big difference between php and asp!


What is the principle of JAVA’s reflection mechanism? It is best to bring an example of how to use the reflection mechanism

Field[] fields = object.getClass().getDeclaredFields();
for (int j = 0; j < fields.length; j++) {

try {
Method method = object.getClass( ).getMethod("get" + name.substring(0, 1).toUpperCase()
+ name.substring(1), new Class[] {});
Object result = method.invoke(object , new Object[] {});
} catch (Exception e) {
e.getStackTrace();
}
}


http://www.bkjia.com/PHPjc/871097.html

truehttp: //www.bkjia.com/PHPjc/871097.htmlTechArticlePHP reflection mechanism usage examples, php reflection examples This article describes the usage of PHP reflection mechanism, share it with everyone for everyone For reference purposes only. The specific method is as follows: The demonstration sample code is as follows...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!