<?php
<?php
trait BindableMethod
{
public function bindMethod($name, Closure $method, $overwrite = true)
{
if (method_exists($this, $name))
{
return;
}
if ($overwrite === false && isset($this->$name))
{
return;
}
$this->$name = \Closure::bind($method, $this, $this);
}
public function __call($method, $args)
{
if (isset($this->$method) && is_callable($this->$method))
{
return call_user_func_array($this->$method, (array) $args);
}
}
}
class Foo
{
use BindableMethod;
private $food = 'Tomatoes';
public function getFood()
{
return $this->food;
}
}
$foo = new Foo();
$foo->bindMethod('changeToPickles', function(){
$this->food = 'pickles';
});
$foo->changeToPickles();
echo $foo->getFood();
- Output for 5.3.0 - 5.3.27, 5.4.0 - 5.4.17
- Parse error: syntax error, unexpected '<' in /in/XFqj6 on line 4
Process exited with code 255.
preferences:
183.58 ms | 1387 KiB | 53 Q