3v4l.org

run code in 300+ PHP versions simultaneously
<?php class salutations { public function hello() { return "Hello World!"; } } class methodFactory { public function __construct($instance) { $this->package = $instance; } public function getPackageMethod($method) { if (method_exists($this->package, $method)) { $f = function($method) use ($this) { return $this->package->$method; }; // Closure::bind($f, $this); return $f; } } } $salutationsInstance = new salutations(); $obj = new methodFactory($salutationsInstance); $hello = $obj->getPackageMethod('hello'); $hello();
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.30, 5.6.8 - 5.6.14
Fatal error: Cannot use $this as lexical variable in /in/OuFuM on line 16
Process exited with code 255.

preferences:
195.59 ms | 1395 KiB | 66 Q