I was wondering if it's OK if I store a set of functions into a array like this
$f = array(
'functions1' =>
array(
'somefunction',
array($this, 'someclassfunction'),
array($this, 'someotherfunction'),
),
'functions2' =>
array(
'somefunction',
array($this, 'someclassfunction'),
array($this, 'someotherfunction'),
),
...
);
and call them later like:
foreach($f as $key => $func)
call_user_func($func)...
If I do a print_r($f)
I get a huge array with lots variables from all these functions. Does this affect performance somehow?
Accepted Answer
If $f
is really big, it'll consume your RAM. print_r
actually prints object properties as well. I can see you use $this multiple times, and that's likely why your output is huge. The PHP documentation reads:
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 is only represented once in memory though, i.e. the performance overhead is low.
call_user_func
itself isn't as fast as calling the function directly though.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki
Comments
Please stop writing tags in titles.