SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Hybrid View

  1. #1
    SitePoint Addict pachanga's Avatar
    Join Date
    Mar 2004
    Location
    Russia, Penza
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP5 objects references question

    After reading last php|architect article about PHP internal implementation of references i still feel that there's one issue left unspoken. That is PHP5 objects internal model. It's obvious they're not just variables...

    How are they implemented actually?

    Consider the following lines in PHP5:

    $a = new Foo(); $b = $a;

    Is it equivalent internally in PHP5 to this:

    $a = new Foo(); $b =& $a;

    If not what the difference? Is it actually "healthy" to use & with objects in PHP5? Is there speed overhead?

    I'm asking all these questions because we're using PHP5 with applications which were written solely for PHP4 yet using OOP. These applications use & almost everywhere and i wonder how PHP5 treats it internally....

    P.S. I addressed the same question to the original author(Derrick Rethans) of this article but being a busy guy(i guess) he didn't respond...

  2. #2
    throw me a bone ... now bonefry's Avatar
    Join Date
    Nov 2004
    Location
    Romania
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    & is ignored. that's it. There was a speed issue in PHP4 when doing something like $obj1 = $obj2 because a copy was made.

    The thing is, right now, & is considered deprecated, and can cause errors because of this

  3. #3
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bonefry
    There was a speed issue in PHP4 when doing something like $obj1 = $obj2 because a copy was made.
    Actually (from http://www.php.net/manual/en/language.oop.newref.php):
    Note: There is no performance loss (since PHP 4 and up use reference counting) returning copies instead of references. On the contrary it is most often better to simply work with copies instead of references, because creating references takes some time where creating copies virtually takes no time (unless none of them is a large array or object and one of them gets changed and the other(s) one(s) subsequently, then it would be wise to use references to change them all concurrently).

  4. #4
    SitePoint Addict pachanga's Avatar
    Join Date
    Mar 2004
    Location
    Russia, Penza
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bonefry
    & is ignored. that's it. There was a speed issue in PHP4 when doing something like $obj1 = $obj2 because a copy was made.
    I'm really not sure if & is ignored when working with objects in PHP5. I'm currently away from my dev. box and can't provide any real examples(i'll try to find some tomorrow) but i do remember that we had to use & explicitly when storing objects in session otherwise objects would get corrupted....

    All this PHP reference thing is so much confusing....

  5. #5
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pachanga
    I'm really not sure if & is ignored when working with objects in PHP5. I'm currently away from my dev. box and can't provide any real examples(i'll try to find some tomorrow) but i do remember that we had to use & explicitly when storing objects in session otherwise objects would get corrupted....

    All this PHP reference thing is so much confusing....
    In theory & is ignored, or should be ignored for objects. However in practice I find it is not. I've found the use of & in places to be backward compatible with PHP4 has been the source of some very hard to track bugs in PHP due to the nasty reference counting bug I mentioned in the other thread.

  6. #6
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The theory is that using & in PHP5 won't make any difference, though as bonefry said, it's "considered deprecated". However, the option remains open to use & to remain consistent with using & with PHP's POD types, and PHP4.

    The reality is that & will create no end of problems in PHP5. I've had no end of trouble using it. It make code hard to debug due to the flawed implementation of references in PHP (This applies to 4 and 5). Of course Zend are trying to rectify this, though it breaks a lot of existing code / compatiblity.

  7. #7
    throw me a bone ... now bonefry's Avatar
    Join Date
    Nov 2004
    Location
    Romania
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    Note: There is no performance loss (since PHP 4 and up use reference counting) returning copies instead of references. On the contrary it is most often better to simply work with copies instead of references, because creating references takes some time where creating copies virtually takes no time (unless none of them is a large array or object and one of them gets changed and the other(s) one(s) subsequently, then it would be wise to use references to change them all concurrently).
    And you actually believe that ? What proofs do they have (or you) ? How can a object copy, that can hold very much data in it, be faster than a reference ?

  8. #8
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bonefry
    And you actually believe that ?
    Well, if we are to start disbelieving documentation, we should all go find a different line of work. I'm not saying that all documentation is always correct and without errors -- all I want to say is that I will consider a documentation correct (although probably incomplete) until proved differently.

  9. #9
    SitePoint Member programmer_php's Avatar
    Join Date
    May 2009
    Location
    India,Mumbai
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    In php5 object assignment is done by reference. So whenever you assign a object to another variable it simply create another reference to the same instance and does not copy the instance.

    so in php 4 if you do this
    $newObj = $oldObj;
    then you have 2 separate objects

    but in php5 if you run same code then $newObje is simply another reference to old object i.e. $oldObj.

    But what if you really want to make separate copy of the object...???

    you can use the clone construct..

  10. #10
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by programmer_php View Post
    You have replied to a 4 year old thread. Don't be surprised if the conversation goes a little stale from here on.

  11. #11
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because it is not an actual copy. Logically you can think of a copy, but physically it is implemented such that the value is not actually copied in memory.

    You should really read this article: http://www.zend.com/zend/art/ref-count.php, it explains very well how copies and references are implemented under the hood

  12. #12
    throw me a bone ... now bonefry's Avatar
    Join Date
    Nov 2004
    Location
    Romania
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Captain Proton
    Because it is not an actual copy. Logically you can think of a copy, but physically it is implemented such that the value is not actually copied in memory.

    You should really read this article: http://www.zend.com/zend/art/ref-count.php, it explains very well how copies and references are implemented under the hood
    Oh I see. They are actually creating references. So if
    $a = 5; $b = $a; $c = $b;
    value "5" is stored in memory and $a, $b, $c all point to the value. And when one variable is changed, it's actually reasigned. Pretty smart actually. The problem is they wanted to switch to the more common "pass reference by value" used in Java, Python and Ruby to make PHP more object oriented.

  13. #13
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bonefry
    Oh I see. They are actually creating references. So if
    $a = 5; $b = $a; $c = $b;
    value "5" is stored in memory and $a, $b, $c all point to the value. And when one variable is changed, it's actually reasigned. Pretty smart actually. The problem is they wanted to switch to the more common "pass reference by value" used in Java, Python and Ruby to make PHP more object oriented.
    Yea, basically it's COW (Copy on Write). This means you can copy an array with many indexes and it will take no time as it is only making references. Only when you change elements will it copy the elements that change. Few people realise this is what happens. That is why you very rarely use references. Generally you only use them on POD types when you want the changes to a variable in a function / method to be reflected in the calling scope.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •