2019-07-14 11:42
浏览 538

PHP - 将匿名函数作为参数传递

Is it possible to pass an anonymous function as a parameter in PHP? And if yes - how?

I am trying to pass an anonymous function to a setter which will fill an array with values returned from that function.

class MyClass
    private $arr = array();

    public function __construct()
        $this->setArrElm('New', function(){return 123;});

    private function setArrElm($name, $val)
        // here: gettype($val) == object
        $this->arr[$name] = $val;

Please note the comment - the type of val is object and I expect an int.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题