开发者

Array values by function

Just a two part question about PHP Arrays (I've read through the comments on php.net, but couldn't see any mention of how this might work.)

Can I have an array populated with functions? For example, using Wordpress:

$arr = array(blog_info('name'), blog_info('stylesheet_url'));

and if so, are these functions called only when $arr[0] or $arr[1] is called, or once the 开发者_Go百科script reaches the array?


These functions are executed as soon as the line containing them is executed. So the array will contain the return values of those two functions.

Using PHP5.3 you could store anonymous functions in it which could be called later:

$arr = array(function() {
    return blog_info('name');
}, function() {
    return blog_info('stylesheet_url');
});

Then you could call $arr[0]() and $arr[1]() later.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜