<?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);
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.
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.