SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    We're from teh basements.
    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 

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,138
    Mentioned
    16 Post(s)
    Tagged
    3 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.

  3. #3
    We're from teh basements.
    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) {
    ...


  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,138
    Mentioned
    16 Post(s)
    Tagged
    3 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.

  5. #5
    We're from teh basements.
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •