php - 函数数组

标签 php arrays class function

我想知道如果我将一组函数存储到这样的数组中是否可以

$f = array(
   'functions1' =>
     array(
      'somefunction',
       array($this, 'someclassfunction'),
       array($this, 'someotherfunction'),
     ),
   'functions2' =>
     array(
      'somefunction',
       array($this, 'someclassfunction'),
       array($this, 'someotherfunction'),
     ),

   ...

);

然后像这样调用他们:

foreach($f as $key => $func)
  call_user_func($func)...

如果我执行 print_r($f),我会得到一个巨大的数组,其中包含来自所有这些函数的大量变量。这会以某种方式影响性能吗?

最佳答案

如果 $f 真的很大,它会占用您的 RAM。 print_r 实际上也打印对象属性。我可以看到您多次使用 $this,这可能就是您的输出量很大的原因。 PHP 文档如下:

print_r(), var_dump() and var_export() will also show protected and private properties of objects with PHP 5. Static class members will not be shown.

虽然 $this 在内存中只表示一次,即性能开销很低。

call_user_func 本身不如直接调用函数快。

关于php - 函数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5929335/

相关文章:

javascript - 使用 Angular.js 和 PHP 在选择下拉列表中遇到问题

php - 根据类别为 WooCommerce 产品标题添加前缀

arrays - 如何按多个元素过滤数组并在 Swift 中找到总和?

c++ - 使用类中的枚举 (C++)

php - Magento/PHP-修改前端产品搜索框下面的MySQL查询

php - jquery .submit() 在 setTimeout 中不会将数据传递给 php

arrays - Delphi 中两个应用程序之间共享数据数组

arrays - 无法修改对象数组中的特定元素

python - Python 中具有特定参数的抽象方法

c++ - C++ 类中的 private const 是多余的吗?