title: Callable and closures
body: <p>I've created not very complex test code (tested in PHP 5.5.12):</p>
  
  <pre><code>&lt;?php
  
  class Test
  {
      private $cached = null;
  
      public function __construct()
      {
          $this-&gt;cached = [];
          $this-&gt;cached[0] = 12;
      }
  
      function wrap($function, $index)
      {
          if (isset($this-&gt;cached[$index])) {
              return $this-&gt;cached[$index];
          }
  
          $result = call_user_func($function);
  
          return $result;
      }
  }
  
  class B
  {
      public function run()
      {
          $x = 6;
          $obj = new Test();
          $value = $obj-&gt;wrap(
              function () use ($x) {
                  return $this-&gt;test($x);
              },
              1
          );
          echo $value."&lt;br /&gt;";
      }
  
  
      protected function test($x)
      {
          echo "I'm running ";
          return $x * $x;
      }
  }
  
  class C extends B
  {
      public function run()
      {
          $x = 6;
          $obj = new Test();
  
          $myFunc = function () use ($x) {
          return $this-&gt;test($x);
          };
  
          $value = $obj-&gt;wrap($myFunc, 1);
          echo $value."&lt;br /&gt;";
      }
  }
  
  class D extends B
  {
      public function run()
      {
          $x = 6;
          $obj = new Test();
  
          $value = $obj-&gt;wrap(array($this, 'test'), 1);
          echo $value."&lt;br /&gt;";
      }
  }
  
  
  $b = new B();
  $b-&gt;run();
  
  $c = new C();
  $c-&gt;run();
  
  $d = new D();
  $d-&gt;run();
  </code></pre>
  
  <p>Probably there are some parts of the code you could say it could be done better but the main point are closures function and callable. Those classes simulate in a very simple way caching system. If data is in cache it returns data from cache otherwise function that gets data is called (of course this cache system doesn't work because it doesn't have to - it's just a sample code).</p>
  
  <p>Questions:</p>
  
  <p>1) Why when using object <code>$d</code> I get the following warning:</p>
  
  <pre><code>call_user_func() expects parameter 1 to be a valid callback, cannot access protected method D::test() 
  </code></pre>
  
  <p>and is it possible to launch protected method from parent? When I change this method from protected to public it can be launched without a problem</p>
  
  <p>2) As you probably noticed I want to use some arguments for function I call using <code>call_user_sync</code>. Unfortunately I don't know those parameters when I call <code>call_user_func</code> so in class B and C I used closures where I can use/pass extra parameters. I have 2 extra questions connected to this:</p>
  
  <ul>
  <li><p>is it the way where closures are useful and commonly used?</p></li>
  <li><p>is it possible using object <code>$d</code> to pass parameters to test method without using closures but not when calling <code>call_user_sync</code> but inside class <code>D</code>? </p></li>
  </ul>