What is the difference between these two class ? [OOP]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aminulsumon
    New Member
    • Dec 2007
    • 5

    What is the difference between these two class ? [OOP]

    I can't understand what is the specialty of & while naming a function. I am not good enough in OOP of PHP.

    [PHP]class FirstClass{
    function &FunctionName($ Para)
    {
    return $Para; //Or any other operation
    }
    }
    class SecondClass{
    function FunctionName($P ara)
    {
    return $Para; //Or any other operation
    }
    }
    $UseClass = &FirstClass::Fu nctionName(10);
    echo $UseClass;
    $UseSecondClass = SecondClass::Fu nctionName(10);
    echo $UseSecondClass ;
    [/PHP]

    Output are same for both classes.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You use the & operator to pass by reference.

    Read about it here

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      markus posted the same link, ignore this post. Took too long to reply...
      Last edited by TheServant; May 21 '08, 06:31 AM. Reason: Took too long to reply...

      Comment

      • coolsti
        Contributor
        • Mar 2008
        • 310

        #4
        Because of a post yesterday, I spent some time reading about the use of the & operator in PHP, and find it can be a bit confusing.

        I knew about passing arguments by references in function calls, but not about assigning class objects as references with =& rather than the usual assignement with = alone. So I have never used this in any of my code.

        But if I am correct, this makes little difference in my scripts. PHP is efficient in the way that when an object is copied using = rather than referred to with =&, no extra memory is used unless the object is modified in some way. So for my scripts, I do not think the use of = rather than =& makes a big difference. Or can someone tell me something to the contrary?

        In my scripts, I have a "table" class that builds up the html for the page, and the table class can contain tables which contain tables. (This is not a memory efficient way to prepare the html output of the script, but by doing it this way, it is extremely easy to set up the page and the classes ensure proper matching of all table, tr and td tags). But I had been concerned that the "=" assignment of tables to other tables for complex pages would eventually eat up all available memory. I found out by examining the used memory after each assignment that this was not the case; assigning a table object to a class variable of another table object with = did not increase the memory at all. Very fortunate.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          PHP is extremely cleaver when it comes to copying variables.

          Even when you use the = operator to copy a variable, PHP will not replicate the variable in the memory until either copy of the variable changes. Until then the second copy will remain a reference to the first one.

          Creating class instances by reference, by using =$, is deprecated in PHP5. The new keyword will do this automatically.
          So, in PHP5:
          [code=php]
          // These are identical
          $first =& new fooClass();
          $first = new fooClass();
          [/code]
          It is noted in the manual.
          Originally posted by PHP.net
          Since PHP 5, new return reference automatically so using =& in this context is deprecated and produces E_STRICT level message.

          Comment

          • coolsti
            Contributor
            • Mar 2008
            • 310

            #6
            Excellant! So all my scripts will then automatically become correct when I migrate over to PHP5!

            :))

            Comment

            • dlite922
              Recognized Expert Top Contributor
              • Dec 2007
              • 1586

              #7
              Originally posted by Atli
              PHP is extremely cleaver when it comes to copying variables.

              Even when you use the = operator to copy a variable, PHP will not replicate the variable in the memory until either copy of the variable changes. Until then the second copy will remain a reference to the first one.

              Creating class instances by reference, by using =$, is deprecated in PHP5. The new keyword will do this automatically.
              So, in PHP5:
              [code=php]
              // These are identical
              $first =& new fooClass();
              $first = new fooClass();
              [/code]
              It is noted in the manual.
              I actually found this out by mistake. It looked like voodoo had its spell on my code, so created a test program and turns out this was exactly true.

              Pass by reference is important though when your passing objects. I use it in my DAO classe so they don't have to create and return a new object. it just modifies the current object and i can continue using it.

              Cheers,

              Dan

              Comment

              Working...