V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
xx19941215
V2EX  ›  问与答

PHP 闭包的调用过程与调用_invoke 方法有关吗?

  •  
  •   xx19941215 · Aug 16, 2017 · 3263 views
    This topic created in 3182 days ago, the information mentioned may be changed or developed.

    读《 Modern PHP 》的时候看到了如上解释,但是 PHP 文档关于闭包的介绍中说道:

    除了此处列出的方法,还有一个 __invoke 方法。这是为了与其他实现了 __invoke()魔术方法 的对象保持一致性,但调用匿名函数的过程与它无关。

    链接

    那 PHP 闭包的调用过程与调用_invoke 方法到底有关吗?

    3 replies    2020-06-04 15:47:01 +08:00
    linoder
        1
    linoder  
       Aug 16, 2017
    无关
    holyghost
        2
    holyghost  
       Aug 16, 2017
    我觉得这两个都翻译错了。

    1. Morden PHP 那句话应该翻译成:「是因为这个变量是一个闭包**对象**」,这个并不太影响上下文理解;

    2. 官方文档的翻译就比较崩溃了,文档原文是:Besides the methods listed here, this class also has an __invoke method. This is for consistency with other classes that implement calling magic, as this method is not used for calling the function.

    你感受下是不是翻译得驴唇不对马嘴。
    chahualao
        3
    chahualao  
       Jun 4, 2020
    中文翻译出来非得换个说法,翻译的人理解了,读者就糊涂了。什么叫“但调用过程与它无关”?
    $closure = function($name){
    echo $name;
    };
    var_dump($closure->__invoke('闭包'));
    结果:
    闭包 NULL
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2367 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 10:33 · PVG 18:33 · LAX 03:33 · JFK 06:33
    ♥ Do have faith in what you're doing.