PHP:遍历所有对象方法(调用对象中的所有方法)

时间:2016-03-15 13:31:13

标签: php

在PHP中,有没有办法以编程方式调用对象的所有方法?

我有一个包含HTML代码段的方法的对象:

class Example {

   public function introduction(){

     ?>

     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

     <?php

  }

  public function step_one(){

     ?>

     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

     <?php

  }

  Etc...
}

有没有办法在循环中按顺序调用所有这些方法?或者我必须通过名字给他们打电话?

2 个答案:

答案 0 :(得分:2)

如果您不知道所述类中将有多少方法,但您知道每种方法的名称基础。然后你可以像这样迭代它们:

<?php
class Example {

   public static function loop() {
    self::introduction();
    $name = "step_one";
    $int = 2;
    $formater = new NumberFormatter("en", NumberFormatter::SPELLOUT);
    while (true) {
        if (method_exists(get_class(), $name)) {
            self::$name();
            $number = $formater->format($int);
            $name = "step_" . $number;
            $int ++;
        }
        else {
            break;
        }
    }
   }

   public static function introduction(){

     ?>

     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

     <?php

  }

  public static function step_one(){

     ?>

     <p>1Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

     <?php

  }

    public static function step_two(){

     ?>

     <p>2Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

     <?php

  }
    public static function step_three(){

     ?>

     <p>3Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

     <?php

  }
}
Example::loop();
?>

或者你可以按照这样的声明顺序遍历它们:

<?php
class Example {

   public function loop() {
    $methods = get_class_methods(get_class());
    foreach ($methods as $method) {
      if ($method != "loop") {
        $this->$method();
      }
    }
   }

   public function introduction(){

     ?>

     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

     <?php

  }

  public function step_one(){

     ?>

     <p>1Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

     <?php

  }

    public function step_two(){

     ?>

     <p>2Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

     <?php

  }
    public function step_three(){

     ?>

     <p>3Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

     <?php

  }
}
$a = new Example();
$a->loop();
?>

答案 1 :(得分:0)

感谢C.Liddell指出我正确的方向。这是我最终在构造函数中做的事情:

public function __construct(){

  $methods = get_class_methods($this);

  foreach($methods as $method) :

    if($method === '__construct') :

      continue;

    endif;

    $this->$method();

  endforeach;

}