Grabbing Arbitrary Amount of Function Arguments by Reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Curtis

    Grabbing Arbitrary Amount of Function Arguments by Reference

    Hello everyone:

    Recently, I've come across the need to accept an arbitrary amount of
    arguments in my function, but I also need to alter the data in the
    calling scope.

    I have been trying to toy around with the func_get_args function,
    however, it seems the data is copied before I can access it, as I
    tried to globalize the values in the argument array and use
    references, but neither worked.

    I would be grateful if anyone could point me in the right direction or
    provide any illumination on this subject.

    Curtis
  • Janwillem Borleffs

    #2
    Re: Grabbing Arbitrary Amount of Function Arguments by Reference

    Curtis wrote:
    I have been trying to toy around with the func_get_args function,
    however, it seems the data is copied before I can access it, as I
    tried to globalize the values in the argument array and use
    references, but neither worked.
    >
    As a workaround, you can pass the function a single array or object by
    reference:

    function modify(&$object ) {
    $object->a = strtoupper($obj ect->a);
    }

    $object = new stdClass;
    $object->a = 'a';
    modify($object) ;
    print $object->a; // A

    Note that when passing an object, it's passed by reference by default in PHP
    5.


    JW


    Comment

    • Curtis

      #3
      Re: Grabbing Arbitrary Amount of Function Arguments by Reference

      Janwillem Borleffs wrote:
      Curtis wrote:
      >I have been trying to toy around with the func_get_args function,
      >however, it seems the data is copied before I can access it, as I
      >tried to globalize the values in the argument array and use
      >references, but neither worked.
      >>
      >
      As a workaround, you can pass the function a single array or object by
      reference:
      >
      function modify(&$object ) {
      $object->a = strtoupper($obj ect->a);
      }
      >
      $object = new stdClass;
      $object->a = 'a';
      modify($object) ;
      print $object->a; // A
      >
      Note that when passing an object, it's passed by reference by default in PHP
      5.
      >
      >
      JW
      >
      >
      Ahh, thank you, I like your approach. Not sure why my mind was set on
      varargs.

      Thanks for the help,
      Curtis, http://dyersweb.com

      Comment

      • Schraalhans Keukenmeester

        #4
        Re: Grabbing Arbitrary Amount of Function Arguments by Reference

        Curtis wrote:
        Hello everyone:
        >
        Recently, I've come across the need to accept an arbitrary amount of
        arguments in my function, but I also need to alter the data in the
        calling scope.
        >
        I have been trying to toy around with the func_get_args function,
        however, it seems the data is copied before I can access it, as I tried
        to globalize the values in the argument array and use references, but
        neither worked.
        >
        I would be grateful if anyone could point me in the right direction or
        provide any illumination on this subject.
        >
        Curtis
        Not so elegant as the solution Janwillem provided, but a useable
        workaround I once used:
        <?PHP
        function foo (&$v1,&$v2,&$v3 ,&$v4,&$v5,&$v6 ,&$v7,&$v8,&$v9 ,&$v10) {
        $count = func_num_args() ;
        for ($i=1;$i<=$coun t;$i++){
        if (gettype(${"v$i "})=='integ er') {
        ${"v$i"}+=1;
        }
        }
        return true;
        }

        $a=1;
        $b=3;
        $c=5;
        $d=8;
        $e='bar';
        @foo ($a,$b,$c,$d,$e ); // suppress warnings about missing parameters
        echo "$a $b $c $d $e";
        // outputs 2 4 6 9 bar
        ?>

        I haven't been able -like you- to alter parameter values when using the
        non_parameteriz ed (is that English?) version of a function and
        func_get_args() . Perhaps with some devious trickery it is possible to
        find the referenced variables somewhere, somehow, but my guess is that
        would not be a pure PHP solution.

        Of ocurse the function is useless, but with some smart
        type-checking-and-branching real nifty stuff could be constructed. I
        haven't tested what the practial parameter list length limit is.

        HTH

        Sh

        Comment

        Working...