References

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

    References

    I guess I don't understand how references work in php;
    Calling a class member function below as an example;
    function &getRow()
    {
    return odbc_fetch_arra y($this->_det);
    }
    I would expect that I should end up with nothing as there is nothing to
    actualy reference.
    If I assign the reference as below;
    $var = getRow()
    and then echo the result;
    echo $var['name'];
    echo $var['address'];
    it all works fine, which surprises me somewhat.
    In other languages I have more experience with this would throw an error, or
    leave you with a reference to nothing, however in php 5 I can use this
    variable hereon without a problem.
    Does php make a copy of the result if there is no var to actualy pass a
    reference to?
    I can't see how it isn't.
    An explaination of this would be very appreciated.
    TIA,
    Vince


  • Jerry Stuckle

    #2
    Re: References

    Vince Morgan wrote:
    I guess I don't understand how references work in php;
    Calling a class member function below as an example;
    function &getRow()
    {
    return odbc_fetch_arra y($this->_det);
    }
    I would expect that I should end up with nothing as there is nothing to
    actualy reference.
    But you do have something to reference. odbc_fetch_arra y returns an
    array, and you are returning a reference to that array.
    If I assign the reference as below;
    $var = getRow()
    and then echo the result;
    echo $var['name'];
    echo $var['address'];
    it all works fine, which surprises me somewhat.
    No surprise at all. It should work that way.
    In other languages I have more experience with this would throw an error, or
    leave you with a reference to nothing, however in php 5 I can use this
    variable hereon without a problem.
    It won't in C++ or Java if odbc_fetch_arra y returns a reference. It
    will work just fine.
    Does php make a copy of the result if there is no var to actualy pass a
    reference to?
    All a variable is is a place to keep something to use later. You don't
    need to use it again in this function, so there's no need to create a
    variable. Just return the array directly, as is being done.
    I can't see how it isn't.
    An explaination of this would be very appreciated.
    TIA,
    Vince
    >
    >



    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Razzbar

      #3
      Re: References

      On May 6, 9:24 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      All a variable is is a place to keep something to use later.
      It would make life simpler if that were so, yes.





      Comment

      • Vince Morgan

        #4
        Re: References

        "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
        news:xMGdnWvEq_ C8eKDbnZ2dnUVZ_ uqvnZ2d@comcast .com...
        Vince Morgan wrote:
        I guess I don't understand how references work in php;
        Calling a class member function below as an example;
        function &getRow()
        {
        return odbc_fetch_arra y($this->_det);
        }
        I would expect that I should end up with nothing as there is nothing to
        actualy reference.
        >
        But you do have something to reference. odbc_fetch_arra y returns an
        array, and you are returning a reference to that array.
        >
        If it passing an array to $var then it isn't behaving as a reference, at
        least not as I understand it. It would be behaving as though I had
        requested passing the array itself in the call wouldn't it?
        No surprise at all. It should work that way.
        Why is that?
        It won't in C++ or Java if odbc_fetch_arra y returns a reference.
        I would have thought that the '&' modifier would do the same in php too
        Jerry.
        All a variable is is a place to keep something to use later. You don't
        need to use it again in this function, so there's no need to create a
        variable. Just return the array directly, as is being done.
        Yes, it is assigning an array evidently, rather than a reference to an array
        so that would mean that the request for a reference it being ignored here?

        Regards,
        Vince


        Comment

        • gosha bine

          #5
          Re: References

          Vince Morgan wrote:
          I guess I don't understand how references work in php;
          Calling a class member function below as an example;
          function &getRow()
          {
          return odbc_fetch_arra y($this->_det);
          }
          I would expect that I should end up with nothing as there is nothing to
          actualy reference.
          Are you debugging with E_ALL? This actually should throw a notice ("only
          variables can be returned by ref" or similar).
          If I assign the reference as below;
          $var = getRow()
          and then echo the result;
          echo $var['name'];
          echo $var['address'];
          it all works fine, which surprises me somewhat.
          In other languages I have more experience with this would throw an error, or
          leave you with a reference to nothing, however in php 5 I can use this
          variable hereon without a problem.
          Does php make a copy of the result if there is no var to actualy pass a
          reference to?
          I can't see how it isn't.
          An explaination of this would be very appreciated.
          TIA,
          Vince
          >
          >


          --
          gosha bine

          extended php parser ~ http://code.google.com/p/pihipi
          blok ~ http://www.tagarga.com/blok

          Comment

          • Vince Morgan

            #6
            Re: References

            "gosha bine" <stereofrog@gma il.comwrote in message
            news:463ee9be$0 $2889$6e1ede2f@ read.cnntp.org. ..
            Vince Morgan wrote:
            I guess I don't understand how references work in php;
            Calling a class member function below as an example;
            function &getRow()
            {
            return odbc_fetch_arra y($this->_det);
            }
            I would expect that I should end up with nothing as there is nothing to
            actualy reference.
            >
            Are you debugging with E_ALL? This actually should throw a notice ("only
            variables can be returned by ref" or similar).
            Yes Gosha. No warning or notice however.

            Vince


            Comment

            • gosha bine

              #7
              Re: References

              Vince Morgan wrote:
              "gosha bine" <stereofrog@gma il.comwrote in message
              news:463ee9be$0 $2889$6e1ede2f@ read.cnntp.org. ..
              >Vince Morgan wrote:
              >>I guess I don't understand how references work in php;
              >>Calling a class member function below as an example;
              >> function &getRow()
              >>{
              >> return odbc_fetch_arra y($this->_det);
              >>}
              >>I would expect that I should end up with nothing as there is nothing to
              >>actualy reference.
              >Are you debugging with E_ALL? This actually should throw a notice ("only
              >variables can be returned by ref" or similar).
              >
              Yes Gosha. No warning or notice however.
              >
              Vince
              >
              >
              Report a bug then. For reference, the followin throws a notice in php 5.2:

              function &foo($rc) {
              return mysql_fetch_arr ay($rc);
              }
              $rc = mysql_query("SE LECT 1");
              $a = foo($rc);
              // NOTICE: Only variable references should be returned by reference


              --
              gosha bine

              extended php parser ~ http://code.google.com/p/pihipi
              blok ~ http://www.tagarga.com/blok

              Comment

              • Vince Morgan

                #8
                Re: References


                "gosha bine" <stereofrog@gma il.comwrote in message
                news:463efba6$0 $2889$6e1ede2f@ read.cnntp.org. ..
                Vince Morgan wrote:
                Are you debugging with E_ALL? This actually should throw a notice
                ("only
                variables can be returned by ref" or similar).
                Yes Gosha. No warning or notice however.

                Vince
                >
                Report a bug then. For reference, the followin throws a notice in php 5.2:
                >
                function &foo($rc) {
                return mysql_fetch_arr ay($rc);
                }
                $rc = mysql_query("SE LECT 1");
                $a = foo($rc);
                // NOTICE: Only variable references should be returned by reference
                I will report it as a bug Gosha.
                The following doesn't report a warning or notice either, which is at least
                consistent;
                function &ret()
                {
                return 1+1;
                }
                $d=ret();
                echo ret;
                Output is 2.

                Thanks,
                Vince




                Comment

                • Jerry Stuckle

                  #9
                  Re: References

                  Razzbar wrote:
                  On May 6, 9:24 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                  >
                  >All a variable is is a place to keep something to use later.
                  >
                  It would make life simpler if that were so, yes.
                  >
                  >
                  >
                  >
                  >
                  No, that's all it is. Nothing more, nothing less.

                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: References

                    Vince Morgan wrote:
                    "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
                    news:xMGdnWvEq_ C8eKDbnZ2dnUVZ_ uqvnZ2d@comcast .com...
                    >Vince Morgan wrote:
                    >>I guess I don't understand how references work in php;
                    >>Calling a class member function below as an example;
                    >> function &getRow()
                    >>{
                    >> return odbc_fetch_arra y($this->_det);
                    >>}
                    >>I would expect that I should end up with nothing as there is nothing to
                    >>actualy reference.
                    >But you do have something to reference. odbc_fetch_arra y returns an
                    >array, and you are returning a reference to that array.
                    >>
                    >
                    If it passing an array to $var then it isn't behaving as a reference, at
                    least not as I understand it. It would be behaving as though I had
                    requested passing the array itself in the call wouldn't it?
                    >
                    You do not "pass" anything to a variable. You pass things to functions,
                    but you *assign* things to an array.

                    All you are doing in your getRow() is returning a reference to the array
                    returned by ordb_fetch_arra y. It is really no different than:

                    function &getRow() {
                    $x = odbc_fetch_arra y($this->_det);
                    return $x;
                    }

                    You just left out the middle step, which is perfectly legitimate.

                    And once you get back, it is assigning that reference to the array to
                    $var.
                    >No surprise at all. It should work that way.
                    >
                    Why is that?
                    >
                    >It won't in C++ or Java if odbc_fetch_arra y returns a reference.
                    >
                    I would have thought that the '&' modifier would do the same in php too
                    Jerry.
                    >
                    You really should watch what you quote. The entire quote was:
                    >In other languages I have more experience with this would throw an
                    >error, or leave you with a reference to nothing, however in php 5 I
                    >can use this variable hereon without a problem.
                    >It won't in C++ or Java if odbc_fetch_arra y returns a reference. It
                    >will work just fine.
                    And it won't cause an error in C++ or Java because it's perfectly valid
                    code. In either case you're returning a reference.
                    >All a variable is is a place to keep something to use later. You don't
                    >need to use it again in this function, so there's no need to create a
                    >variable. Just return the array directly, as is being done.
                    >
                    Yes, it is assigning an array evidently, rather than a reference to an array
                    so that would mean that the request for a reference it being ignored here?
                    >
                    Regards,
                    Vince
                    >
                    >
                    What do you expect to be different? Operations on a reference are
                    exactly the same as operations on a copy. The *only* difference in your
                    code is that if you change a reference, you change the original. if you
                    change a copy, you do not.

                    And if you're just accessing (and not changing) the any values, you have
                    no way of telling if you're acting on a copy or a reference, unless you
                    go back to see how it was passed or returned.


                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Jerry Stuckle

                      #11
                      Re: References

                      gosha bine wrote:
                      Vince Morgan wrote:
                      >"gosha bine" <stereofrog@gma il.comwrote in message
                      >news:463ee9be$ 0$2889$6e1ede2f @read.cnntp.org ...
                      >>Vince Morgan wrote:
                      >>>I guess I don't understand how references work in php;
                      >>>Calling a class member function below as an example;
                      >>> function &getRow()
                      >>>{
                      >>> return odbc_fetch_arra y($this->_det);
                      >>>}
                      >>>I would expect that I should end up with nothing as there is nothing to
                      >>>actualy reference.
                      >>Are you debugging with E_ALL? This actually should throw a notice ("only
                      >>variables can be returned by ref" or similar).
                      >>
                      >Yes Gosha. No warning or notice however.
                      >>
                      >Vince
                      >>
                      >>
                      >
                      Report a bug then. For reference, the followin throws a notice in php 5.2:
                      >
                      function &foo($rc) {
                      return mysql_fetch_arr ay($rc);
                      }
                      $rc = mysql_query("SE LECT 1");
                      $a = foo($rc);
                      // NOTICE: Only variable references should be returned by reference
                      >
                      >
                      Gosha,

                      But you're dealing with a different function, so the results may vary.

                      Take the following script in 5.2:

                      <?php

                      function &getArrayRef () {
                      static $ar = array();
                      return $ar;
                      }

                      function getArrayCopy() {
                      static $ar = array();
                      return $ar;
                      }

                      function &getGetArrayRef () {
                      return getArrayRef();
                      }

                      function &getGetArrayCop y() {
                      return getArrayCopy();
                      }

                      echo "Return by ref:\n";
                      $var1 = getGetArrayRef( );
                      echo "Return by copy:\n";
                      $var2 = getGetArrayCopy ();

                      ?>

                      Output:

                      Return by ref:
                      Return by copy:

                      Notice: Only variable references should be returned by reference in
                      test.php on line 18

                      Notice that when getArrayRef returns a reference, getGetArrayRef can
                      return that reference with no notice. However, since getArrayCopy
                      returns a copy, getGetArrayCopy gets a NOTICE when it tries to return
                      the reference.

                      It looks like odbc_fetch_arra y is acting like the former, and
                      mysql_fetch_arr ay is acting like the latter. The former way is
                      obviously more efficient (and flexible). But changing an array member
                      may have undesired effects. It's hard to know when you don't know what
                      the code that created the array is doing.

                      --
                      =============== ===
                      Remove the "x" from my email address
                      Jerry Stuckle
                      JDS Computer Training Corp.
                      jstucklex@attgl obal.net
                      =============== ===

                      Comment

                      • gosha bine

                        #12
                        Re: References

                        Vince Morgan wrote:
                        "gosha bine" <stereofrog@gma il.comwrote in message
                        news:463efba6$0 $2889$6e1ede2f@ read.cnntp.org. ..
                        >Vince Morgan wrote:
                        >
                        >>>Are you debugging with E_ALL? This actually should throw a notice
                        ("only
                        >>>variables can be returned by ref" or similar).
                        >>Yes Gosha. No warning or notice however.
                        >>>
                        >>Vince
                        >>>
                        >>>
                        >Report a bug then. For reference, the followin throws a notice in php 5.2:
                        >>
                        >function &foo($rc) {
                        >return mysql_fetch_arr ay($rc);
                        >}
                        >$rc = mysql_query("SE LECT 1");
                        >$a = foo($rc);
                        >// NOTICE: Only variable references should be returned by reference
                        >
                        I will report it as a bug Gosha.
                        The following doesn't report a warning or notice either, which is at least
                        consistent;
                        It does on my system. Before filing a bug, make sure 1) you're using php
                        5.2.0 or later and 2) you have proper error_reporting and display_errors
                        settings.

                        function &ret()
                        {
                        return 1+1;
                        }
                        $d=ret();
                        echo ret;
                        Output is 2.
                        >
                        Thanks,
                        Vince
                        >
                        >
                        >
                        >

                        --
                        gosha bine

                        extended php parser ~ http://code.google.com/p/pihipi
                        blok ~ http://www.tagarga.com/blok

                        Comment

                        • Vince Morgan

                          #13
                          Re: References

                          "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
                          news:Ht2dnUWaW4 HkvqLbnZ2dnUVZ_ gydnZ2d@comcast .com...
                          Vince Morgan wrote:
                          "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
                          news:xMGdnWvEq_ C8eKDbnZ2dnUVZ_ uqvnZ2d@comcast .com...
                          Vince Morgan wrote:
                          >I guess I don't understand how references work in php;
                          >Calling a class member function below as an example;
                          > function &getRow()
                          >{
                          > return odbc_fetch_arra y($this->_det);
                          >}
                          >I would expect that I should end up with nothing as there is nothing
                          to
                          >actualy reference.
                          But you do have something to reference. odbc_fetch_arra y returns an
                          array, and you are returning a reference to that array.
                          >
                          If it passing an array to $var then it isn't behaving as a reference, at
                          least not as I understand it. It would be behaving as though I had
                          requested passing the array itself in the call wouldn't it?
                          >
                          You do not "pass" anything to a variable. You pass things to functions,
                          but you *assign* things to an array.
                          >
                          Yes, I'm sorry it's getting late here Jerry.
                          All you are doing in your getRow() is returning a reference to the array
                          returned by ordb_fetch_arra y. It is really no different than:
                          >
                          function &getRow() {
                          $x = odbc_fetch_arra y($this->_det);
                          return $x;
                          }
                          >
                          You just left out the middle step, which is perfectly legitimate.
                          >
                          And once you get back, it is assigning that reference to the array to
                          $var.
                          Actualy, it seems to be assigning a copy to $var rather than a reference to
                          it.
                          A simple test seems to verify this.

                          <?
                          function &get()
                          {
                          global $r;
                          return $r;
                          }
                          $r=5;
                          $var=$r;
                          echo $var;//output is 5 as expected.
                          $r=6;
                          echo $var;//output is still 5 as not expected, by me that is ;)
                          $var=$r;
                          echo $var;//output is now 6
                          ?>

                          If it was actualy a reference to $r that was being assigned, the second echo
                          should also be 6.
                          On my machine with 5.0.33 the output is as described above.
                          You really should watch what you quote. The entire quote was:
                          >
                          Yes, you are right Jerry, My appologies. I plead the late thing again.
                          >In other languages I have more experience with this would throw an
                          >error, or leave you with a reference to nothing, however in php 5 I
                          >can use this variable hereon without a problem.
                          >
                          >It won't in C++ or Java if odbc_fetch_arra y returns a reference. It
                          >will work just fine.
                          >
                          Yes, you are correct on both accounts. I didn't read the "if
                          odbc_fetch_arra y returns a reference" bit correctly.

                          I wouldn't expect odbc_etc() to return a reference, without at least
                          declaring this in the docs, but then again I'm still learning.

                          Regards,
                          Vince


                          Comment

                          • Vince Morgan

                            #14
                            Re: References

                            "Vince Morgan" <vinharAtHereop tusnet.com.auwr ote in message
                            news:463f3216$0 $15845$afc38c87 @news.optusnet. com.au...
                            <?
                            function &get()
                            {
                            global $r;
                            return $r;
                            }
                            $r=5;
                            $var=$r;
                            echo $var;//output is 5 as expected.
                            $r=6;
                            echo $var;//output is still 5 as not expected, by me that is ;)
                            $var=$r;
                            echo $var;//output is now 6
                            ?>
                            Oooops, getting too late evidently. I meant to write the following.
                            <?
                            function &get()
                            {
                            global $r;
                            return $r;
                            }
                            $r=5;
                            $var=get();
                            echo $var;//output is 5 as expected.
                            $r=6;
                            echo $var;//output is still 5
                            $var=get();
                            echo $var;//output is now 6
                            ?>


                            Comment

                            • Vince Morgan

                              #15
                              Re: References

                              "gosha bine" <stereofrog@gma il.comwrote in message
                              news:463f2998$0 $2891$6e1ede2f@ read.cnntp.org. ..
                              Vince Morgan wrote:
                              "gosha bine" <stereofrog@gma il.comwrote in message
                              I will report it as a bug Gosha.
                              The following doesn't report a warning or notice either, which is at
                              least
                              consistent;
                              >
                              It does on my system. Before filing a bug, make sure 1) you're using php
                              5.2.0 or later and 2) you have proper error_reporting and display_errors
                              settings.
                              Error_reporting is "error_reportin g = E_ALL", and "display_er rors = On"
                              I need to upgrade. I'm using 5.0.33.
                              Thank you Gosha,
                              Vince


                              Comment

                              Working...