0

Let's imagine that class:

Class Foo {

   function1($p1, $p2)
  {
    do_common_thing1()
    specific_1($p1)
    specific_2($p2)
    do_common_thing2()
  }
  function2($p1)
  {
    do_common_thing1()
    specific_3($p1)
    do_common_thing2()
  }
  /**   ...    */
  function99()
  {
    do_common_thing1()
    specific_999()
    do_common_thing2()
  }
}

How can I code the class to avoid repeating the common function in each method? I've heard of the template method pattern but it seems like it can't apply to ALL my methods

1 Answer 1

1

Consider this:

protected function doWithCommon($func) {
    do_common_thing1();
    $func();
    do_common_thing2();
}

public function func1() {
    $this->doWithCommon(function() {
        // specific
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.