ibhhvc
ibhhvc

Reputation: 265

PHP: Iterate through all object methods (call all methods in object)

In PHP, is there any way to call all methods of an object programmatically?

I have an object full of methods that contain snippets of 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...
}

Is there any way to call all of these methods in order in a loop? Or do I have to call them all by name?

Upvotes: 3

Views: 1212

Answers (2)

ibhhvc
ibhhvc

Reputation: 265

Thanks to C.Liddell for pointing me in the right direction. Here's what I ended up doing in my constructor:

public function __construct(){

  $methods = get_class_methods($this);

  foreach($methods as $method) :

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

      continue;

    endif;

    $this->$method();

  endforeach;

}

Upvotes: 1

C.Liddell
C.Liddell

Reputation: 1082

If you don't know how many methods are going to be in said class, but you know the base of the name for each method. Then you can iterate through them like this:

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

Alternatively you can loop through them in order of declaration like this:

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

Upvotes: 4

Related Questions