SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Hybrid View
-
Apr 5, 2009, 07:53 #1
- Join Date
- Apr 2007
- Posts
- 1,205
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Legal to pass reference to declared global as default function argument?
I have a function that requires as arguments a reference to an array and a reference to a scalar variable. This function passes those arguments to another function, which supplies them to a callback function when it gets executed at a later time, e.g., via call_user_func_args. In some cases, the callback may not require an array of arguments or return a value. Therefore, I want to provide an empty array and a null scalar as default arguments. My question is, is it legal to use a variable's name as a default argument in the function's declaration, as shown in this code?
PHP Code:$empty_array = array();
$null_output = null;
function add_idle($callback,&$args = $empty_array,&$ret = $null_output) {
try {
$handle = rand_str(16);
$idle = IdleTaskList::getInstance();
$idle->addTask($handle,$callback,&$args,&$ret);
return $handle;
}
catch (Exception $e) {
alert($e->getMessage());
return false;
}
} // add_idle
-
Apr 5, 2009, 08:16 #2
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
no
You also need to supply those referenced arguments. References must be supplied in that context.
There may exists some 5.3 voodoo to get that to work, but as far as I know it's not legal.
-
Apr 5, 2009, 08:20 #3
- Join Date
- Apr 2007
- Posts
- 1,205
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How would you do it? Would it be best to leave those arguments out of the signature and use func_num_args and func_get_args in the function to decide whether anything has been passed? Or would this work?
PHP Code:function add_idle($callback,&$args = array(),&$ret = null) {
...
}
-
Apr 5, 2009, 08:26 #4
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
That will work apparently.
func_get_args() doesn't return references regardless if they are specified as such in the parameters. So I don't think that would work for you if you need references.
-
Apr 5, 2009, 08:30 #5
- Join Date
- Apr 2007
- Posts
- 1,205
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'll do some tests and see what happens. Thanks!
Bookmarks