3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A { function __construct($val) { $this->val = $val; } function getClosure() { //returns closure bound to this object and scope return function() { return $this->val; }; } } $ob1 = new A(1); $ob2 = new A(2); echo $ob1->getClosure()->call($obj1); echo $ob2->getClosure()->call($ob2);
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Creation of dynamic property A::$val is deprecated in /in/4APXg on line 5 Deprecated: Creation of dynamic property A::$val is deprecated in /in/4APXg on line 5 Warning: Undefined variable $obj1 in /in/4APXg on line 16 Fatal error: Uncaught TypeError: Closure::call(): Argument #1 ($newThis) must be of type object, null given in /in/4APXg:16 Stack trace: #0 /in/4APXg(16): Closure->call(NULL) #1 {main} thrown in /in/4APXg on line 16
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
Warning: Undefined variable $obj1 in /in/4APXg on line 16 Fatal error: Uncaught TypeError: Closure::call(): Argument #1 ($newThis) must be of type object, null given in /in/4APXg:16 Stack trace: #0 /in/4APXg(16): Closure->call(NULL) #1 {main} thrown in /in/4APXg on line 16
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: obj1 in /in/4APXg on line 16 Warning: Closure::call() expects parameter 1 to be object, null given in /in/4APXg on line 16 2
Output for 7.3.32 - 7.3.33
Warning: Closure::call() expects parameter 1 to be object, null given in /in/4APXg on line 16 2
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Fatal error: Call to undefined method Closure::call() in /in/4APXg on line 16
Process exited with code 255.

preferences:
170.66 ms | 402 KiB | 183 Q