Example of application of reflection in PHP

Source: Internet
Author: User
  1. /**
  2. * PHP Reflection Instance
  3. * Edit bbs.it-home.org
  4. */
  5. Class person{
  6. Public $name;
  7. function __construct ($name) {
  8. $this->name= $name;
  9. }
  10. }
  11. Interface module{
  12. function execute ();
  13. }
  14. Class Ftpmodule implements module{
  15. function Sethost ($host) {
  16. Print "Ftpmodule::sethost (): $host \ n";
  17. }
  18. function SetUser ($user) {
  19. Print "Ftpmodule::setuser (): $user \ n";
  20. }
  21. function Execute () {
  22. Something
  23. }
  24. }
  25. Class Personmodule implements module{
  26. function Setperson (person $person) {
  27. Print "personmodule::setperson:{$person->name}\n";
  28. }
  29. function Execute () {
  30. Something
  31. }
  32. }
  33. Class modulerunner{
  34. Private $configData
  35. =array (
  36. "Personmodule" =>array (' person ' = ' bob '),
  37. "Ftpmodule" =>array (' host ' = ' example.com ', ' user ' = ' anon ')
  38. );
  39. Private $modules =array ();
  40. function init () {
  41. $interface =new reflectionclass (' Module ');
  42. foreach ($this->configdata as $modulename = + $params) {
  43. $module _class=new Reflectionclass ($modulename);//Instantiate Reflectionclass based on the name of the configuration ConfigData
  44. if (! $module _class->issubclassof ($interface)) {//check reflection to see if the class is a subclass of $interface
  45. throw new Exception ("Unknown module type: $modulename");//Not a module subclass throws an exception
  46. }
  47. $module = $module _class->newinstance ();//Instantiate a ftpmodule or Personmodule object
  48. foreach ($module _class->getmethods () as $method) {//Get Methods in class
  49. $this->handlemethod ($module, $method, $params);
  50. }
  51. Array_push ($this->modules, $module);//Place the instantiated module object into the $modules array
  52. }
  53. }
  54. function Handlemethod (Module $module, Reflectionmethod $method, $params) {
  55. $name = $method->getname ();//Get Method name
  56. $args = $method->getparameters ();//Get the parameters in the method
  57. if (count ($args)!=1| | substr ($name, 0,3)! = "Set") {//The check method must start with set and only one parameter
  58. return false;
  59. }
  60. $property =strtolower (substr ($name, 3));//The method name is removed set three letters, as a parameter
  61. if (!isset ($params [$property])) {//If the $params array does not contain a property, it returns false
  62. return false;
  63. }
  64. $arg _class=@ $args [0]->getclass;//Check the data type of the first parameter (and unique) of the setter method
  65. if (Empty ($arg _class)) {
  66. $method->invoke ($module, $params [$property]);
  67. }else{
  68. $method->invoke ($module, $arg _class->newinstance ($params [$property]);
  69. }
  70. }
  71. }
  72. $test =new Modulerunner ();
  73. $test->init ();
  74. ?>
Copy Code

Second, the information of the class is obtained by reflection:

    1. /**
    2. * PHP Reflection gets the information of the class
    3. * Edit bbs.it-home.org
    4. */
    5. Class reflectionutil{
    6. static function Getclasssource (Reflectionclass $class) {
    7. $path = $class->getfilename ();
    8. $lines = @file ($path);
    9. $from = $class->getstartline ();
    10. $to = $class->getendline ();
    11. $len = $to-$from +1;
    12. Return implode (Array_slice ($lines, $from-1, $len));
    13. }
    14. }
    15. $classname = "Person";
    16. $path = ". /practice/{$classname}.php ";
    17. if (!file_exists ($path)) {
    18. throw new Exception ("No such file as {$path}");
    19. }
    20. Require_once ($path);
    21. if (!class_exists ($classname)) {
    22. throw new Exception ("No such class as {$classname}");
    23. }
    24. Print Reflectionutil::getclasssource (new Reflectionclass (' person '));
    25. ?>
Copy Code

Output Result:

Class person{public $age, public $name, function GetName () {return ' ZJX ';} function Getage () {return;} function __tostri Ng () {$rs = $this->getname (); $rs. = "(age". $this->getage (). ")"; return $rs;}}
  • 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.