Using func_get_args() as second (or more) parameter

Permanent Link: Using func_get_args() as second (or more) parameter 29. April 2009 Comment No Comment

There's a strange bug in PHP we recently came across: When using func_get_args() is used as second (or third or fourth etc) parameter, PHP crashes with a Fatal Error.

Try this code:

function returnSomething()
{
        return func_get_args();
}

function callSomeFunction($message)
{
        someFunction(func_get_args(), 'Hello World');
        someFunction(1, returnSomething());
        someFunction(1, func_get_args());
}

function someFunction($code, $message)
{
        print($message . PHP_EOL);
}

callSomeFunction('Hello World');

The result should be

Hello World
Array
Array

yet you get

Hello World
Array

Fatal error: func_get_args(): Can't be used as a function parameter
in /home/dominik/test.php on line 12

The code works with any other function (see example code), only func_get_args() causes a crash.

So I thought: Let's commit a bug! I then found out that this very bug has already been committed in 2005: http://bugs.php.net/bug.php?id=27887. The bug was closed (unfixed) with this really unsatisfactory explanation:

[16 Jun 2005 1:34am UTC] tony2001@php.net
Damien, the docs say: "This function cannot be used directly as a function
parameter. Instead, its result may be assigned to a variable, which can
then be passed to the function.", so don't use it as a parameter.

[16 Jun 2005 9:04am UTC] derick@php.net

So there is no bug, and the docs already describe it -> bogus.

Of course: Why should I fix a bug, when I can just put a note in the documentation that says "don't use it as parameter".

Edit: I filed a bug and it was closed with the comment "This is a known issue and have been fixed in 5.3.". Good to know!