title: The correct way of doing delegates or callbacks in PHP
body: <p>I need to implement the following pattern in php:</p>
  
  <pre><code>class EventSubscriber
  {
      private $userCode;
      public function __construct(&amp;$userCode) { $this-&gt;userCode = &amp;$userCode; }
      public function Subscribe($eventHandler) { $userCode[] = $eventHandler; }
  }
  
  class Event
  {
      private $subscriber;
      private $userCode = array();
  
      public function __construct()
      {
          $this-&gt;subscriber = new Subscriber($this-&gt;userCode)
      }
  
      public function Subscriber() { return $this-&gt;subscriber; }
  
      public function Fire()
      {
          foreach ($this-&gt;userCode as $eventHandler)
          {
              /* Here i need to execute $eventHandler  */
          }
      }
  }
  
  class Button
  {
      private $eventClick;
  
      public function __construct() { $this-&gt;eventClick = new Event(); }
  
      public function EventClick() { return $this-&gt;eventClick-&gt;Subscriber(); }
  
      public function Render()
      {
          if (/* Button was clicked */) $this-&gt;eventClick-&gt;Fire();
  
          return '&lt;input type="button" /&gt;';
      }
  }
  
  class Page
  {
      private $button;
  
      // THIS IS PRIVATE CLASS MEMBER !!!
      private function ButtonClickedHandler($sender, $eventArgs)
      {
          echo "button was clicked";
      }
  
      public function __construct()
      {
          $this-&gt;button = new Button();
          $this-&gt;button-&gt;EventClick()-&gt;Subscribe(array($this, 'ButtonClickedHandler'));
      }
  
      ...
  
  }    
  </code></pre>
  
  <p>what is the correct way to do so.</p>
  
  <p>P.S.</p>
  
  <p>I was using call_user_func for that purpose and believe it or not it was able to call private class members, but after few weeks of development i've found that it stopped working. Was it a bug in my code or was it some something else that made me think that 'call_user_func' is able call private class functions, I don't know, but now I'm looking for a simple, fast and elegant method of safely calling one's private class member from other class. I'm looking to closures right now, but have problems with '$this' inside closure...</p>