473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

copy of object is a reference in PHP5

Can someone please explain the results below
We switched to PHP 5
if I make a copy of an object, and then change the variable inside the
object the change is reflected in the copy.

Either the copy is a reference to the object or the variable inside
the object is a static variable, I'm not sure what is going on, but It
doesn't seem like it should be doing that. Is that because of a
setting on the server or something ?

<?
$label=new myObj();
$label->settext('the text');

$copy=$label;
print_r($copy);
$label->settext('different text');
print_r($copy);


class myObj{
var $text;
function settext($text){
$this->text=$text;
}

}

?>
results:
myObj Object
(
[text] =the text
)
myObj Object
(
[text] =different text
)

Oct 25 '07 #1
3 1587
On Oct 25, 11:50 am, grou...@reenie.org wrote:
Can someone please explain the results below
We switched to PHP 5
if I make a copy of an object, and then change the variable inside the
object the change is reflected in the copy.

Either the copy is a reference to the object or the variable inside
the object is a static variable, I'm not sure what is going on, but It
doesn't seem like it should be doing that. Is that because of a
setting on the server or something ?

<?

$label=new myObj();
$label->settext('the text');

$copy=$label;
print_r($copy);
$label->settext('different text');
print_r($copy);

class myObj{
var $text;
function settext($text){
$this->text=$text;
}

}

?>
results:
myObj Object
(
[text] =the text
)
myObj Object
(
[text] =different text
)
All objects in PHP 5, if I'm not mistaken, are always passed by
reference. Example:
<?php

class MyClass
{
public $myVar;

function __construct()
{
$this->myVar = 1;
}
}

$class1 = new MyClass();

$class2 = $class1;

$class1->myVar = 2;

echo $class2->myVar; // Outputs 2

?>

If you want to make a copy of an object, so its a new object and not a
reference is to use the clone keyword like this:

<?

class MyClass
{
public $myVar;

function __construct()
{
$this->myVar = 1;
}
}

$class1 = new MyClass();

$class2 = clone $class1;

$class1->myVar = 2;

echo $class2->myVar;

?>

You can also explicitly code what is to happen when an object is
cloned by using the __clone() class function. You can read more here:
http://us3.php.net/manual/en/language.oop5.cloning.php

Carmony

Oct 25 '07 #2
gr*****@reenie.org schrieb:
On Oct 25, 2:05 pm, JustinCarmony <carm...@gmail.comwrote:
>You can also explicitly code what is to happen when an object is
cloned by using the __clone() class function.
$label=new geocellobj();
$label->setalign('right');
array($label->settext('unique test'), $label->settext('different
text));
I don't think cloning will help me.
Well, just clone the objects before storing them into the array:

array(clone $label->settext(...), ...)
Oct 25 '07 #3
On Oct 25, 6:17 pm, Thomas Hamacher <da...@nurfuerspam.dewrote:
grou...@reenie.org schrieb:
On Oct 25, 2:05 pm, JustinCarmony <carm...@gmail.comwrote:
You can also explicitly code what is to happen when an object is
cloned by using the __clone() class function.
$label=new geocellobj();
$label->setalign('right');
array($label->settext('unique test'), $label->settext('different
text));
I don't think cloning will help me.

Well, just clone the objects before storing them into the array:

array(clone $label->settext(...), ...)
Ok thanks that works great.

Oct 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
4
by: Kevin | last post by:
Hi all, I've got a PHP4 app that I developed which I'm trying to get to run on a PHP5 server. Everything works great, except for one thing. There's a particular routine that creates an...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
1
by: | last post by:
any body please help me ,Please explain what is a shallow copy and what is a deep copy?
5
by: lion | last post by:
in .net, if you set annstance-A of a class equal to another instance-B, a pointer will add to B, but if i want to create a copy of B instead of pointer, how to operate? Note:serialization...
1
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.