Object$references

Discussion in 'PHP' started by fluteofliar, Jul 4, 2010.

  1. #1
    i am bit confuse about.. between passing object with refernces and passing object without references.
    As i read in php manual..
    "A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
    then whats a difference between these two statement

    $obj1=$obj2;//since we are copying object identifier here thus both points to same area in memory where object reside

    and $obj1=&$obj2;//here too both points to same location in memory where object reside
     
    fluteofliar, Jul 4, 2010 IP
  2. fluteofliar

    fluteofliar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    AFA i am thinking the differences is same as between pointer and reference?
     
    fluteofliar, Jul 4, 2010 IP
  3. RobiMac

    RobiMac Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In PHP 4.x the object was passed by Value (copy), unless prefixed by an ampersand (&). In PHP 5.x(not sure exactly which version in the 5 branch), this has changed such that a copy of the object (reference) is passed by default and if you want a copy of the object, you use the clone keyword, which if memory serves, provides a shallow copy. The best reference on this is the PHP manual.
     
    RobiMac, Jul 4, 2010 IP
  4. c_programmer

    c_programmer Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    They are roughly equivalent. They don't call them pointers because PHP does not directly access the server's memory, rather an abstraction of it created by the core engine. They are the same for most common purposes, you just arent directly messing with memory (you can easily crash things when you do).
     
    c_programmer, Jul 4, 2010 IP
  5. fluteofliar

    fluteofliar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    can you please explain this process in details or give me a link to read about it
    from many days i am searching about this but didn,t find anything:(
     
    fluteofliar, Jul 4, 2010 IP
  6. fluteofliar

    fluteofliar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    what i found,
    $obj1=$obj2//here if i change the value of obj2(change object handle or identifier),its ll not reflect any change in obj1,so they just behave like pointer(though not a correct word,acc to you)..pointing to same object

    and $obj1=&$obj2;//here if i change the value of any object the other ll reflect the change too..,here both are references,changing one will reflect the change in other too
     
    fluteofliar, Jul 4, 2010 IP
  7. c_programmer

    c_programmer Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Best example I can think of was another topic on another forum where a similar question came up. This one is my post, it is a more technical explanation. This one is another member's post, it is a littler easier to read.

    While we are talking about pointers, the concept is the same even if the PHP engine does it a little differently behind the scenes. You're not alone in this problem, I'm yet to find a programmer who did not struggle with this concept at one point or another.

    Its not that they reflect each other, they are accessing the same portion of the computer's memory. Any number of variables can access a single portion of memory space.
     
    Last edited: Jul 4, 2010
    c_programmer, Jul 4, 2010 IP
  8. fluteofliar

    fluteofliar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i have read the links , now plz tell me where i am wrong...
    class hello
    { function bye()
    {
    echo "confuse";
    }}
    $obj1=new bye;//1.here first the object is stored in memeory and then the identifier or handle of that object ll pass to obj1 to acess the object
    $b=10;

    echo "$b"; /*2.as i read $b ll point to location in ram where value 10 is stored..using $b means we are accessing that memory location and comp internally provide us the value stored at that address(this process is compl transparent to us)*/

    $obj1->bye();/*3.obj1 pointing to memory location where identifier(object handle) is stored,for using obj1 comp internally provide us that handler which used
    to access or call the bye function of identified object(by handler),thus in abstraction we can say obj1 point to location where actual object is stored*/

    $obj2=$obj1;/*4.here we are passing the copy of handle to obj2,thus obj2 also points to same memory location of actual object,here obj2 and obj1 both are depend on central object(where actual data stored)but they are independent on each other
    since changing the content of one ll only divert the direction of one obj to point to some other location*/

    $obj3=&$obj1;/*5.here as usual obj1 pointing to memory location of actual object,this statement passing the address where the value of obj1 stored into obj3,this means both obj1 and obj3 are depend on central object ,but if we change the content(handle) of obj1
    ...then if you say*/
    $obj4=$obj3;/*5(2).obj4 contain the address of obj3,here though it seems changing value of obj3 doesn,t bring any chnage in obj1(thus both are independent)...
    but if we try to access a data member(whose value has been changed after changing content of obj1) with obj3,we access the chnaged value thus both are dependent on each other */

    thus in both cases we cans say they are sharing common memory because both are dependent on the data placed at center location or of actual object
     
    fluteofliar, Jul 4, 2010 IP
  9. c_programmer

    c_programmer Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Don't use objects here, they are not straight forward when it comes to memory.

    Basically it works like this (this is not completely correct, but I am not discussing internal memory management):
    1 2 3. The class is copied to that variable
    4. You are replicating the entire class and creating a copy at another point in memory.
    5. obj2 points to obj 1, obj 1 for the purposes of this contains its own data.
    5(2). You are assigning it by value, not address.

    Your problem here is that $var=new className; is not a pointer.
     
    c_programmer, Jul 4, 2010 IP
  10. fluteofliar

    fluteofliar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    this is what php manual say,
    and this is what you says
    both seems contradict to me,yeah i did mistake at 5(2).
    3.one more ques,does php being interpreted lang use lazy-copy method?
     
    fluteofliar, Jul 4, 2010 IP
  11. c_programmer

    c_programmer Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    It still does not behave completely like a reference.

    I have no idea.
     
    c_programmer, Jul 5, 2010 IP