From:             
Operating system: Not Relevant
PHP version:      Irrelevant
Package:          Reflection related
Bug Type:         Feature/Change Request
Bug description:String arguments for func_get_arg() as well as func_has_arg()

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).


-- 
Edit bug report at https://bugs.php.net/bug.php?id=60568&edit=1
-- 
Try a snapshot (PHP 5.4):            
https://bugs.php.net/fix.php?id=60568&r=trysnapshot54
Try a snapshot (PHP 5.3):            
https://bugs.php.net/fix.php?id=60568&r=trysnapshot53
Try a snapshot (trunk):              
https://bugs.php.net/fix.php?id=60568&r=trysnapshottrunk
Fixed in SVN:                        
https://bugs.php.net/fix.php?id=60568&r=fixed
Fixed in SVN and need be documented: 
https://bugs.php.net/fix.php?id=60568&r=needdocs
Fixed in release:                    
https://bugs.php.net/fix.php?id=60568&r=alreadyfixed
Need backtrace:                      
https://bugs.php.net/fix.php?id=60568&r=needtrace
Need Reproduce Script:               
https://bugs.php.net/fix.php?id=60568&r=needscript
Try newer version:                   
https://bugs.php.net/fix.php?id=60568&r=oldversion
Not developer issue:                 
https://bugs.php.net/fix.php?id=60568&r=support
Expected behavior:                   
https://bugs.php.net/fix.php?id=60568&r=notwrong
Not enough info:                     
https://bugs.php.net/fix.php?id=60568&r=notenoughinfo
Submitted twice:                     
https://bugs.php.net/fix.php?id=60568&r=submittedtwice
register_globals:                    
https://bugs.php.net/fix.php?id=60568&r=globals
PHP 4 support discontinued:          
https://bugs.php.net/fix.php?id=60568&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=60568&r=dst
IIS Stability:                       
https://bugs.php.net/fix.php?id=60568&r=isapi
Install GNU Sed:                     
https://bugs.php.net/fix.php?id=60568&r=gnused
Floating point limitations:          
https://bugs.php.net/fix.php?id=60568&r=float
No Zend Extensions:                  
https://bugs.php.net/fix.php?id=60568&r=nozend
MySQL Configuration Error:           
https://bugs.php.net/fix.php?id=60568&r=mysqlcfg

Reply via email to