获得一系列功能的兄弟姐妹

Kev*_*ans 3 php

有没有办法在函数内部获取函数的兄弟函数?

我有一系列功能,类似于:

$thingy = [
  'do_something' => function() {
    // call $thingy['do_something_else']()?
  },

  'do_something_else' => function() {

    return 1234;
  }
];
Run Code Online (Sandbox Code Playgroud)

有没有办法可以打电话do_something_else给我do_something?在其他语言中,例如javascript,您应该能够使用this或可以使用变量名称thingy.

Ama*_*ali 6

是.您可以$thingy通过引用传递来实现此目的:

$thingy = [
    'do_something' => function() use (&$thingy) {
        echo $thingy['do_something_else'](); // just an example
    },
    'do_something_else' => function() {
        return 1234;
    }
];
Run Code Online (Sandbox Code Playgroud)

用法示例:

$thingy['do_something']();
Run Code Online (Sandbox Code Playgroud)

输出:

1234
Run Code Online (Sandbox Code Playgroud)

演示