|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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).
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 06 10:00:01 2025 UTC |
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)