title: Hooking method execution in PHP
body: <p>Given a base class where I have some logic that must run before and after a certain method, that takes wildly different parameters in its differing derived classes.
  As an example:</p>
  
  <pre><code>abstract class Base{    
      public function pre(){ print "Runing Base::pre()\n"; }
      public function pos(){ print "Runing Base::post()\n"; }
      abstract public function doIt();
  }
  
  class Cheating extends Base
  {
      public function doIt(){
          $this-&gt;pre();
          print "Doing it the cheating way\n";
          $this-&gt;pos();
      }
  }
  </code></pre>
  
  <p>But what I'd actually like to do is along the lines of:</p>
  
  <pre><code>class Alpha extends Base
  {
      public function doIt($x, $y){
          print "Doing it in Alpha with x=$x, y=$y";
      }
  }
  
  class Beta extends Base
  {
      public function doIt($z){
          print "Doing it in Alpha with z=$z";
      }
  }
  </code></pre>
  
  <p>And have some way to always run the pre and pos methods, without having to change the doIt method itself.</p>
  
  <p>The obvious way, should doIt be homogenous across all derivations of Base would be something like:</p>
  
  <pre><code>abstract class Base
  {
      public function pre(){ print "Runing Base::pre()\n"; }
      public function pos(){ print "Runing Base::post()\n"; }
  
      public function process($a,$b){
          $this-&gt;pre();
          $this-&gt;doIt($a,$b);
          $this-&gt;pos();
      }
  
      abstract public function doIt();
  }
  
  class Wishful extends Base
  {
      public function doIt($a, $b){
          print "Doing it the Wishful way with a=$a, b=$b\n";
      }
  }
  </code></pre>
  
  <p>The problem there is that since we have different number and type of parameters on each implementation of doIt, it does not actually solve the problem.</p>
  
  <p>This sounds like a case for "hooks" - how does one go about implementing those ? Or any other decent way of solving the problem... I believe there should be a simpler way that I'm just missing - probably thinking in circles one the wrong things.</p>
  
  <p>Thanks!</p>