title: Calling method X from each parent
body: <p>what i'm trying to do is call each method "init" from current class's parents.</p>
  
  <p>I'm doing that to avoid programmers to have to call init method (parent::init()) each time they create an init method in a new controller.</p>
  
  <p>Example:</p>
  
  <pre><code>class Aspic\Controller { } // main controller
  class ControllerA extends Aspic\Controller { public function init() {/* do something 1 */}  
  class ControllerB extends ControllerA {}
  class ControllerC extends ControllerB { public function init() { /* do something 2 */ }
  class ControllerD extends ControllerC {}
  </code></pre>
  
  <p>As you can see the <code>init</code> methods do not call parent <code>init</code> method but i want my app (there is an option) do it.</p>
  
  <p>Thus when I'm loading <code>ControllerD</code>, before calling it's <code>init</code> method (there isn't in the example but the app test it), i want to call each parent <code>init</code> method.</p>
  
  <p>sound like this:</p>
  
  <pre><code>parent::init(); // Controller C init
  parent::parent::parent::init(); // Controller A init
  </code></pre>
  
  <p>So i did :</p>
  
  <pre><code>if($this-&gt;_autoCallParentsInit) {
      // Aspic\Controller is the main controller, which is the mother of all others
      $aspicControllerRc = new \ReflectionClass('Aspic\\Controller');
      $rc = new \ReflectionClass($this); // We are in D
      $currPrefix = '';
  
      // Calling each init methods of current class parent
      // Avoid using parent::init() in each controller
      while(($parentClass = $rc-&gt;getParentClass()) AND $aspicControllerRc-&gt;isInstance($parentClass)) {
          /* 
          $aspicControllerRc-&gt;isInstance($parentClass)
          =&gt; because Aspic\Controller extends a "Base class". Thus, we stopped at Aspic\Controller
          */
  
          $currPrefix .= 'parent::';
  
          // Must have explicit method (not inherited from parent) BUT actually hasMethod does not care
          if($parentClass-&gt;hasMethod('init')) {
              call_user_func($currPrefix.'init');                     
          }
      }
  }
  </code></pre>
  
  <p>This is not working because <code>ReflectionClass::isInstance</code> does not accept others argument than the object we want to test (and the not a <code>ReflectionClass</code> object representing it as in the example)</p>
  
  <p>**
  Simply:
  I have an object $x, and i want to call the <code>init</code> method of each parent of the class of $x.
  **</p>
  
  <p>Is it possible ?</p>
  
  <p>I hope i was clear :)</p>
  
  <p>Thanks</p>