2011-02-12 36 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