Trying to implement pre/post method code: better to use methods or a parent class?
<p>I'm finding it difficult to frame this question so ... I want to execute code both before and after a method runs in PHP. There are, as far as I know, two ways to implement this:</p>

<h3>Method One: pre and post methods</h3>

<pre><code>class Model
{
  function find($id)
  {
    $this-&gt;_precode();
    // ... do stuff
    $this-&gt;post_code();
  }
}
</code></pre>

<p>Add the calls to <code>_precode()</code> and <code>_postcode()</code> to each method where I need this functionality.</p>

<h3>Method Two: __call and method naming</h3>

<pre><code>class Model extends PrePost
{
  function prepost_find($id)
  {
    // ... do stuff ...
  }
}

class PrePost 
{
  function __call($method,$param) 
  {
    $method = "prepost_$method";
    // .. precode here ..
    $result = $this-&gt;$method($param);
    // .. postcode here ..
  }
}
</code></pre>

<p>This relies on naming a method in a specific way in the inheriting class. Is there a preferred way of doing this? The call method can be made to only handle its specific cases and even defer to a child class's call if there is one.</p>

<p>I'm not looking for opinions; I'm looking to find out if there are valid reasons to choose one way over another.</p>
