Guest User

Untitled

a guest
Apr 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.84 KB | None | 0 0
  1.     // How am I supposed to call this correctly?
  2.     // I want to be free to use closures for c1::cb...
  3.  
  4.     function f1()
  5.     {
  6.         echo "1";
  7.     }
  8.    
  9.     class c1
  10.     {
  11.         public $cb = f1;
  12.         public function callcb()
  13.         {
  14.             // this works:
  15.             $cb = $this->cb;
  16.             $cb();
  17.             // the following versions don't:
  18.             $this->cb(); // Call to undefined method c1::cb()
  19.             ${$this->cb}(); // Function name must be a string (this is the one which IMO really should work)
  20.             $this->$cb(); // Function name must be a string (even though I expected it to resolve to c1::f1 and fail because of that
  21.             $this->{$this->cb}(); // Call to undefined method c1::f1
  22.             {$this->cb}(); // } unexpected (obviously)
  23.             ${this->cb}(); // unexpected T_OBJECT_OPERATOR
  24.  
  25.             // Note: variants 2 and 3 don't work even with public $cb='f1'.
  26.         }
  27.     }
  28.     $c = new c1;
  29.     $c->callcb();
  30.     $page->end();
Add Comment
Please, Sign In to add comment