Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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



Report this thread to moderator Post Follow-up to this message
Old Post
Oli Filth
10-27-04 08:56 PM


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 changin
g
> that behaviour?
>
> Oli
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
Harrie Verveer
10-27-04 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:47 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.