gpt4 book ai didi

php - 函数数组

转载 作者:可可西里 更新时间:2023-10-31 22:47:58 26 4
gpt4 key购买 nike

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

$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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com