3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A { private $name; public function __construct($name) { $this->name = $name; } } // test callable function getName() { return $this->name; } $bob = new A("Bob"); $cl1 = Closure::fromCallable("getName"); $cl1 = $cl1->bindTo($bob, 'A'); //This will retrieve: Uncaught Error: Cannot access private property A::$name $result = $cl1(); echo $result; //But for a Lambda function $cl2 = function() { return $this->name; }; $cl2 = $cl2->bindTo($bob, 'A'); $result = $cl2(); // This will print Bob echo $result;
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Warning: Cannot rebind scope of closure created from function in /in/tq0lO on line 19 Fatal error: Uncaught Error: Value of type null is not callable in /in/tq0lO:22 Stack trace: #0 {main} thrown in /in/tq0lO on line 22
Process exited with code 255.
Output for 7.4.14 - 7.4.33
Warning: Cannot rebind scope of closure created from function in /in/tq0lO on line 19 Fatal error: Uncaught Error: Function name must be a string in /in/tq0lO:22 Stack trace: #0 {main} thrown in /in/tq0lO on line 22
Process exited with code 255.
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.13
Warning: Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure() in /in/tq0lO on line 19 Fatal error: Uncaught Error: Function name must be a string in /in/tq0lO:22 Stack trace: #0 {main} thrown in /in/tq0lO on line 22
Process exited with code 255.
Output for 7.0.0 - 7.0.24
Fatal error: Uncaught Error: Call to undefined method Closure::fromCallable() in /in/tq0lO:18 Stack trace: #0 {main} thrown in /in/tq0lO on line 18
Process exited with code 255.

preferences:
148.01 ms | 401 KiB | 176 Q