Advertisement
Guest User

Untitled

a guest
Aug 27th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  1. <?php
  2.  
  3.     class Test extends Thread
  4.     {
  5.         public $callbackVar;
  6.        
  7.         public function run()
  8.         {
  9.             $this->callbackVar->__invoke();
  10.         }
  11.        
  12.         public function setClosure(Closure $callback)
  13.         {
  14.             $this->callbackVar = $callback;
  15.         }
  16.        
  17.         public function setDeclarativeClosure()
  18.         {
  19.             $this->callbackVar = function()
  20.             {
  21.                 echo "Declarative closure";
  22.             };
  23.         }
  24.        
  25.         public function rewrapClosure(Closure $callback)
  26.         {
  27.             $localCallback = function($c)
  28.             {
  29.                 $_c = $c;
  30.                 return function() use ($_c)
  31.                 {
  32.                     $_c();  
  33.                 };
  34.             };
  35.            
  36.             $this->callbackVar = $localCallback($callback);
  37.         }
  38.     }
  39.    
  40.     error_reporting(0);
  41.    
  42.     $callbackVar = function()
  43.     {
  44.         echo "Callback var invoked.";
  45.     };
  46.    
  47.     $test = new Test();
  48.    
  49.     $test->setClosure($callbackVar);
  50.     assert($test->callbackVar === null, 'Callback is not null.');
  51.     $test->callbackVar->__invoke();
  52.     $test->start();
  53.    
  54.     $test->setDeclarativeClosure();
  55.     assert($test->callbackVar === null, 'Callback is not null.');
  56.     $test->callbackVar->__invoke();
  57.     $test->start();
  58.    
  59.     $test->rewrapClosure($callbackVar);
  60.     assert($test->callbackVar === null, 'Callback is not null.');
  61.     $test->callbackVar->__invoke();
  62.     $test->start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement