3

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?

2
  • 1
    You can use get_class_methods to get the method names from a class, but the concept of "order" should be tossed out. You defined the methods, but PHP is allowed to put them in any order it likes. Commented Mar 15, 2016 at 13:33
  • Other than storing each HTML snippet in a HEREDOC array value, is there any way to "order" them? Commented Mar 15, 2016 at 13:35

2 Answers 2

3

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();
?>
Sign up to request clarification or add additional context in comments.

Comments

1

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;

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.