繁体   English   中英

PHP闭包 - 如何用$ this调用类函数

[英]PHP Closures - How do I call a class function with $this

我在使用PHP闭包时遇到了一些麻烦。

好吧,让我们说我有:

$router->bind('~/~', function()
{
    print "I'm on the home page";
});

$shel = new Shel($config, $router);
$shel->start();

现在,Shel的所有功能都被调用了。 在Shel里面,有一个函数load()。 有没有办法从我绑定的闭包调用Shel :: load(),使用$ this?

干杯!

PHP 5.3: https//wiki.php.net/rfc/closures/object-extension

对于PHP 5.3 $,这种对闭包的支持被删除了,因为无法达成共识如何以理智的方式实现它。 此RFC描述了在下一个PHP版本中可以实现它的可能道路。

所以在PHP 5.3中你必须解决一下:

$that = $this;
$router->bind('~/~', function() use ($that)
{
    print "I'm on the home page";
});

对于5.4,您只需使用$ this。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM