Advertisement
Guest User

PHP wrong context execution

a guest
Jul 18th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. <?php
  2.  
  3. class Base
  4. {
  5.     public function testBase()
  6.     {
  7.         var_dump(__METHOD__ . ' is ran from: ' . get_class($this));
  8.  
  9.         return $this->x;
  10.     }
  11. }
  12.  
  13. class Aclass extends Base
  14. {
  15.     private $x = __CLASS__;
  16.  
  17.     public function test()
  18.     {
  19.         var_dump(__METHOD__ . ' is ran as: ' . get_class($this));
  20.  
  21.         return $this->x;
  22.     }
  23. }
  24.  
  25. class Bclass
  26.     extends Aclass
  27. {
  28.     private $x = __CLASS__;
  29. }
  30.  
  31.  
  32. $bObj = new Bclass();
  33. var_dump($bObj, $bObj->test());
  34.  
  35. echo '===========' . PHP_EOL;
  36.  
  37. \Closure::bind(function() { return $this->test(); }, new Aclass(), $bObj)->__invoke();
  38. //force a context change
  39. \Closure::bind(function() { return $this->test(); }, $bObj, $bObj)->__invoke();
  40.  
  41. echo '===========' . PHP_EOL;
  42.  
  43. $aObj = new Aclass();
  44. $aObj->testBase();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement