Passing an array as a reference to a function

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

    #1

    Passing an array as a reference to a function

    Noobie here (C++/C/Java experience though) ...

    Recently picked up PHP ...

    I want to pass an array to a function and then to use count on the
    passed variable - is this the way to do it (its an object method) :

    public function foo(array& $arr)
    {
    if (count($arr) < 1)
    throw new MyException("Bl ah blah ...") ;
    ....
    }


    I have two questions for the pros:

    1). Is the function signature correct (valid syntax?)
    2). Can count accept a reference type variable ?

  • countach

    #2
    Re: Passing an array as a reference to a function

    En las nuevas, el Bit Byte escribió:
    Noobie here (C++/C/Java experience though) ...
    >
    Recently picked up PHP ...
    >
    I want to pass an array to a function and then to use count on the
    passed variable - is this the way to do it (its an object method) :
    >
    public function foo(array& $arr)
    {
    if (count($arr) < 1)
    throw new MyException("Bl ah blah ...") ;
    ....
    }
    >
    >
    I have two questions for the pros:
    >
    1). Is the function signature correct (valid syntax?)
    2). Can count accept a reference type variable ?
    1) Correct syntax:

    public function foo(&$arr)
    {
    if (count($arr) < 1)
    throw new MyException("Bl ah blah ...") ;
    ....
    }

    2) Yes


    Comment

    • Robin

      #3
      Re: Passing an array as a reference to a function

      Bit Byte wrote:
      1). Is the function signature correct (valid syntax?)
      Depends on your PHP-Version, i.e.:

      PHP 4:
      function foo(&$arr){}

      PHP 4 knows nothing about visibilty and always passes by value, hence
      the '&' to signal a pass-by-reference. Nice text about using objects
      and references in PHP 4:
      Discover powerful applications such as Little Snitch Mini, Little Snitch, LaunchBar and Micro Snitch.


      PHP 5:
      public function foo(array $arr){}

      PHP 5 always passes by reference, so no '&' is needed.

      Cheers
      Robin

      Comment

      • Colin Fine

        #4
        Re: Passing an array as a reference to a function

        Robin wrote:
        Bit Byte wrote:
        >1). Is the function signature correct (valid syntax?)
        >
        Depends on your PHP-Version, i.e.:
        >
        PHP 4:
        function foo(&$arr){}
        >
        PHP 4 knows nothing about visibilty and always passes by value, hence
        the '&' to signal a pass-by-reference. Nice text about using objects
        and references in PHP 4:
        Discover powerful applications such as Little Snitch Mini, Little Snitch, LaunchBar and Micro Snitch.

        >
        PHP 5:
        public function foo(array $arr){}
        >
        PHP 5 always passes by reference, so no '&' is needed.
        >
        Cheers
        Robin
        I don't believe this is correct.

        http://www.php.net/manual/en/functions.arguments.php says:

        "By default, function arguments are passed by value (so that if you
        change the value of the argument within the function, it does not get
        changed outside of the function). If you wish to allow a function to
        modify its arguments, you must pass them by reference.

        "If you want an argument to a function to always be passed by reference,
        you can prepend an ampersand (&) to the argument name in the function
        definition:"

        I think that what you are thinking of is that in PHP5 *objects* are
        always passed by reference, but arrays are not objects.

        Therefore the & *is* still needed in PHP5.

        Furthermore, I can't find where it says so, but I'm pretty sure that the
        PHP5 extension that allows you to specify a type for a function argument
        is limited to objects.

        If I am right, then :

        for an array in PHP5

        public function foo(&$arr) {

        but if you use an object class MyArray

        public function foo (MyArray $arr) {

        Colin



        Comment

        • Bit Byte

          #5
          Re: Passing an array as a reference to a function



          Colin Fine wrote:
          Robin wrote:
          >
          >Bit Byte wrote:
          >>
          >>1). Is the function signature correct (valid syntax?)
          >>
          >>
          >Depends on your PHP-Version, i.e.:
          >>
          >PHP 4:
          >function foo(&$arr){}
          >>
          >PHP 4 knows nothing about visibilty and always passes by value, hence
          >the '&' to signal a pass-by-reference. Nice text about using objects
          >and references in PHP 4:
          >http://www.obdev.at/developers/articles/00002.html
          >>
          >PHP 5:
          >public function foo(array $arr){}
          >>
          >PHP 5 always passes by reference, so no '&' is needed.
          >>
          >Cheers
          >Robin
          >
          >
          I don't believe this is correct.
          >
          http://www.php.net/manual/en/functions.arguments.php says:
          >
          "By default, function arguments are passed by value (so that if you
          change the value of the argument within the function, it does not get
          changed outside of the function). If you wish to allow a function to
          modify its arguments, you must pass them by reference.
          >
          "If you want an argument to a function to always be passed by reference,
          you can prepend an ampersand (&) to the argument name in the function
          definition:"
          >
          I think that what you are thinking of is that in PHP5 *objects* are
          always passed by reference, but arrays are not objects.
          >
          Therefore the & *is* still needed in PHP5.
          >
          Furthermore, I can't find where it says so, but I'm pretty sure that the
          PHP5 extension that allows you to specify a type for a function argument
          is limited to objects.
          >
          If I am right, then :
          >
          for an array in PHP5
          >
          public function foo(&$arr) {
          >
          but if you use an object class MyArray
          >
          public function foo (MyArray $arr) {
          >
          Colin
          >
          >
          >
          Thanks all. This is a very detailed (and very useful) reply.

          Comment

          Working...