2011-02-12 41 views
9

我已经看过PHP手册。但我不明白早期版本和更高版本的PHP之间的行为差​​异。我不明白这样的说法:func_num_args,func_get_arg和func_get_args从php 5.2到5.3的行为差异

Because this function depends on the current scope to determine parameter details, it cannot be used as a function parameter in versions prior to 5.3.0. If this value must be passed, the results should be assigned to a variable, and that variable should be passed.

回答

11

如果您想将其中一个函数的结果传递给另一个函数或方法,那么在5.3之前的PHP版本中,您必须首先将结果赋值给一个变量。

function some_func() { 
    $args = func_get_args(); 
    some_other_func($args); 
} 

在PHP 5.3中删除了这个限制,您现在可以直接传递结果。

function some_func() { 
    some_other_func(func_get_args()); 
} 

至于为什么摆在首位存在这种限制,也许有人用PHP的内部的更透彻的理解可以给你一个更完整的答案。

+0

它可能与PHP在调用函数的范围和它传递给它的函数之间真的混淆了。 – BoltClock

+0

在一些非常奇怪的情况下,它似乎也可以工作:http://codepad.org/MQkQnnJH – cmbuckley

8

这意味着,这是无效的5.2:

function foo() { 
    $array = array_map('strtolower', func_get_args()); 
} 
foo('BAR', 'BAZ'); 

它将与一个致命错误中止:

PHP Fatal error: func_get_args(): Can't be used as a function parameter

在5.3

然而,它是有效的代码。

+0

对于Googlable错误消息的+1 – Daniel