This is a discussion on Class objects in PHP 5.0 within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi In PHP 4, the first line below creates a complete (deep) copy of a class object, the second creates ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi
In PHP 4, the first line below creates a complete (deep) copy of a class object, the second creates a reference copy: $j = $i; // creates deep copy $j =& $i; // creates reference That makes sense, it's the same for scalars. But, can anyone explain to me the rationale behind the PHP developers' decision that in PHP 5, the syntax of the first line now also creates a reference copy, and breaking loads of existing code in the process? We now have the situation illustrated below: $j = $i; // creates copy if $i is a scalar type $j = $i; // creates reference if $i is a class object $j =& $i; // creates reference whatever $i is $j = clone $i; // creates copy (clone) if $i is a class object Now, object copying syntax is inconsistent within PHP, and also does not reflect the behaviour in languages like C++. What was the point in changing that behaviour? Oli |
|
|||
|
This is a wild guess, but most of less experienced php programmers don't
use the &-sign ever, so in fact they are copying the most gigantic objects without meaning to. When people do something like $i=$j and $j is an object, 99% of the occassions they just want $i to be a pointer to $j (and they should type $i=&$j). What I'm trying to say: referencing to an object is used very more often than copying it, so I'd say it makes sence to make that the default behaviour. Programming languages like Java and Flash actionscript (:P) do exactly the same by the way. Oli Filth wrote: > Hi > > In PHP 4, the first line below creates a complete (deep) copy of a class > object, the second creates a reference copy: > > $j = $i; // creates deep copy > $j =& $i; // creates reference > > That makes sense, it's the same for scalars. > > But, can anyone explain to me the rationale behind the PHP developers' > decision that in PHP 5, the syntax of the first line now also creates a > reference copy, and breaking loads of existing code in the process? We now > have the situation illustrated below: > > $j = $i; // creates copy if $i is a scalar type > $j = $i; // creates reference if $i is a class object > $j =& $i; // creates reference whatever $i is > $j = clone $i; // creates copy (clone) if $i is a class object > > Now, object copying syntax is inconsistent within PHP, and also does not > reflect the behaviour in languages like C++. What was the point in changing > that behaviour? > > Oli > > |