dttwois6098 2012-02-08 01:50
浏览 58
已采纳

是否可以在类范围中定义匿名函数?

I'd like to assign anonymous functions to arrays in PHP inside of a class, but I keep stumbling over syntax errors.

class Stuff {
  private $_preference_defaults = array(
    'cookie'    => true,
    'session'   => true,
    'database'  => true,
    'filter'    => function($input) { return true; },
    'sanitizer' => function($input) { return $input; },
  );
};

Will throw an unexpected T_FUNCTION syntax error, and it doesn't matter whether or not the array is static. I got desperate and tried the "old" way of doing it:

class Stuff {
  private $_preference_defaults = array(
    'cookie'    => true,
    'session'   => true,
    'database'  => true,
    'filter'    => create_function('$input', 'return true;'),
    'sanitizer' => create_function('$input', 'return $input;'),
  );
};

And this led to an unexpected '(', expecting ')' syntax error. However, if I define the functions ahead of time, it does work:

class Stuff {
  function _preference_default_filter($input) {
    return true;
  }
  function _preference_default_sanitizer($input) {
    return true;
  }
  private $_preference_defaults = array(
    'cookie'    => true,
    'session'   => true,
    'database'  => true,
    'filter'    => _preference_default_filter,
    'sanitizer' => _preference_default_sanitizer,
  );
};

And then I'm able to call those functions within a class method:

function do_stuff($foo) {
  return $this->{$this->_preference_defaults['filter']}($foo);
}

Not only is this syntactically sour, in my head it wreaks of poor style and I can imagine this sort of trickery causing headaches in the future.

  1. Am I not able to create an anonymous function at class (static) scope? (I'm thinking maybe because a valid Closure object cannot be created in that context?)
  2. Is there a more... PHP... means to the same end? That is to say, is there a "better" way of assigning trivial default callbacks to array values in class scope? The final snippet I provided gets the job done for sure, but I'd prefer a solution that won't leave me asking the optometrist for a stronger prescription.
  • 写回答

3条回答 默认 最新

  • drlma06060 2012-02-08 01:54
    关注

    Am I not able to create an anonymous function at class (static) scope? (I'm thinking maybe because a valid Closure object cannot be created in that context?)

    Yes, because the initial values of class attributes should be known in parsing step, while function call and anonymous function require runtime

    Also see here to get the additional details: https://stackoverflow.com/a/9029556/251311

    And cannot get your second question :-S

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题