[英]PHP Closures - How do I call a class function with $this
I'm having a little trouble with PHP Closures. 我在使用PHP闭包时遇到了一些麻烦。
Okay, so let's say I have: 好吧,让我们说我有:
$router->bind('~/~', function()
{
print "I'm on the home page";
});
$shel = new Shel($config, $router);
$shel->start();
Now, all my functions are called by Shel. 现在,Shel的所有功能都被调用了。 Inside Shel, there's a function load(). 在Shel里面,有一个函数load()。 Is there a way to call Shel::load() from the closure that I've binded, using $this? 有没有办法从我绑定的闭包调用Shel :: load(),使用$ this?
Cheers! 干杯!
PHP 5.3: https://wiki.php.net/rfc/closures/object-extension PHP 5.3: https : //wiki.php.net/rfc/closures/object-extension
For PHP 5.3 $this support for Closures was removed because no consensus could be reached how to implement it in a sane fashion. 对于PHP 5.3 $,这种对闭包的支持被删除了,因为无法达成共识如何以理智的方式实现它。 This RFC describes the possible roads that can be taken to implement it in the next PHP version. 此RFC描述了在下一个PHP版本中可以实现它的可能道路。
So in PHP 5.3 you had to workaround a bit: 所以在PHP 5.3中你必须解决一下:
$that = $this;
$router->bind('~/~', function() use ($that)
{
print "I'm on the home page";
});
For 5.4 you can use just $this. 对于5.4,您只需使用$ this。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.