admin管理员组文章数量:1200788
In JavaScript, I can use apply
to pass an array as arguments to a function:
var f = function (n,m) {},
args = [1,2];
f.apply(null, args);
I now need to do something similar in PHP i.e. pass an array of items as 'separate' arguments to a function.
Is there any way this can be done?
In JavaScript, I can use apply
to pass an array as arguments to a function:
var f = function (n,m) {},
args = [1,2];
f.apply(null, args);
I now need to do something similar in PHP i.e. pass an array of items as 'separate' arguments to a function.
Is there any way this can be done?
Share Improve this question asked Aug 14, 2011 at 22:51 Andreas GrechAndreas Grech 108k101 gold badges303 silver badges362 bronze badges 6- you can just parse the array as the argument – user557846 Commented Aug 14, 2011 at 23:01
- Sure, but it's much cleaner to pass them as separate arguments. – Andreas Grech Commented Aug 14, 2011 at 23:07
- is it? don't see why, there's a place for both i guess. – user557846 Commented Aug 14, 2011 at 23:12
- 4 Dagon: having worked on a big codebase that uses arrays pretty much everywhere for arguments, I can tell you it's a LOT easier when it's simple arguments. It's self documenting for one thing.. – Evert Commented Aug 15, 2011 at 1:26
- 1 @NickyDeMaeyer even more work to worry about 2 year old threads :-) – user557846 Commented Jul 4, 2013 at 8:59
1 Answer
Reset to default 25You can use the function call_user_func_array
. Simply pass in your function (as a callback, usually a string with the function name), and an array of arguments.
Additional note: for static functions, use forward_static_call_array
.
本文标签: Is there something like JavaScript39s apply function in PHPStack Overflow
版权声明:本文标题:Is there something like JavaScript's apply function in PHP? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738595945a2101786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论