SitePoint Sponsor |
|
User Tag List
Results 1 to 13 of 13
Thread: PHP5 objects references question
-
Jul 16, 2005, 02:30 #1
- 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...
-
Jul 16, 2005, 11:01 #2
- 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
-
Jul 16, 2005, 11:35 #3
- 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.
-
Jul 16, 2005, 12:38 #4
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by bonefry
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).
-
Jul 17, 2005, 01:24 #5
- Join Date
- Mar 2004
- Location
- Russia, Penza
- Posts
- 265
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by bonefry
All this PHP reference thing is so much confusing....
-
Jul 17, 2005, 01:36 #6
- Join Date
- Nov 2004
- Location
- Romania
- Posts
- 848
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by BerislavLopac
-
Jul 17, 2005, 02:24 #7
- 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
-
Jul 17, 2005, 03:27 #8
- Join Date
- Nov 2004
- Location
- Romania
- Posts
- 848
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Captain Proton
$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.
-
Jul 17, 2005, 04:14 #9
- Join Date
- Jul 2004
- Location
- Gerodieville Central, UK
- Posts
- 446
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by bonefry
-
Jul 17, 2005, 04:16 #10
- Join Date
- Jul 2004
- Location
- Gerodieville Central, UK
- Posts
- 446
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by pachanga
-
Jul 17, 2005, 04:49 #11
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by bonefry
-
May 6, 2009, 06:41 #12
- Join Date
- May 2009
- Location
- India,Mumbai
- Posts
- 2
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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..
-
May 6, 2009, 06:52 #13
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Bookmarks