Home > Archive > PHP Language > October 2004 > Class objects in PHP 5.0
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Class objects in PHP 5.0
|
|
| Oli Filth 2004-10-27, 3:56 pm |
| 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
| |
| Harrie Verveer 2004-10-27, 3:56 pm |
| 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
>
>
|
|
|
|
|