Class objects in PHP 5.0

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-27-2004
Oli Filth
 
Posts: n/a
Default Class objects in PHP 5.0

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


Reply With Quote
  #2 (permalink)  
Old 10-27-2004
Harrie Verveer
 
Posts: n/a
Default Re: Class objects in PHP 5.0

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
>
>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:14 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0