php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60568 String arguments for func_get_arg() as well as func_has_arg()
Submitted: 2011-12-20 03:37 UTC Modified: 2011-12-20 09:20 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: dan dot lugg at gmail dot com Assigned:
Status: Open Package: Reflection related
PHP Version: Irrelevant OS: Not Relevant
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dan dot lugg at gmail dot com
New email:
PHP Version: OS:

 

 [2011-12-20 03:37 UTC] dan dot lugg at gmail dot com
Description:
------------
The following are my proposals:

func_get_arg($arg_num) should support string arguments. A given string argument 
should correspond to the parameter name:

    function test($foo){
        var_dump(func_get_arg(0));
        var_dump(func_get_arg('foo'));
    }

    test('hello');
    // string(5) "hello"
    // string(5) "hello"

As well, an additional function should be added; func_has_arg($arg_num_or_name) 
which will *not* throw warnings when an out-of-bounds argument is passed. It 
should accept the same argument types of int|string as func_get_arg()

    function test($foo){
        var_dump(func_has_arg(0));
        var_dump(func_has_arg('foo'));
        var_dump(func_has_arg(1));
        var_dump(func_has_arg('bar'));
    }

    test('hello');
    // bool(true)
    // bool(true)
    // bool(false)
    // bool(false)

I find that without this functionality, it is very difficult to determine 
whether or not an argument has in fact been passed in some circumstances. This 
issue was brought to light when I was attempting to test for an optional 
argument intended to be passed by reference.

Like the preg_* family of functions, the optional parameter by reference would 
be populated with values under certain conditions of the function call. The 
parameter was declared with a default value of NULL. The issue is, when passing 
a previously undefined variable as an argument to this function, it has the 
inherent value of NULL, making is_null($arg) fail.

One may consider unconditionally populating the variable with results, however 
if the process to generate these results is expensive, then there would be a 
clear advantage to testing whether the client-code has explicitly requested the 
results before proceeding with the necessary process.

My colleagues and I have determined that some solutions may include:

 - Adding an additional boolean flag argument to a given function, and proceed 
with populating the referenced variable on a TRUE condition.
 - Checking instead, the number of arguments passed with func_num_args().
 - Generating some arbitrary value with low possibility of collision, defining 
it as a constant, and using that as a default. 

Unfortunately, these solutions are mere work-arounds. The additional boolean 
flag lengthens the parameter list unnecessarily. Checking the number of passed 
arguments couples the check to the parameter count. The arbitrary default value 
is fraught with issues from collision to maintenance and overhead.

These issues would be mitigated by supporting string arguments to func_get_arg() 
and the proposed func_has_arg(), by decoupling the functions from parameter 
count/order through the support for named parameter queries, as well as allowing 
for a genuine check of whether an argument was in fact passed (regardless of 
value, default or not).


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-20 09:20 UTC] dan dot lugg at gmail dot com
To expand on the func_has_arg() functionality -- As in the case of func_get_arg(), 
func_has_arg() should return FALSE for non-supplied arguments, regardless of any 
default values:

    function test($foo, $bar = null){
        var_dump(func_has_arg(0));
        var_dump(func_has_arg('foo'));
        var_dump(func_has_arg(1));
        var_dump(func_has_arg('bar'));
    }

    test('hello');
    // bool(true)
    // bool(true)
    // bool(false)
    // bool(false)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:29 2024 UTC