copy of object is a reference in PHP5
copy of object is a reference in PHP5
am 25.10.2007 19:50:14 von meltedown
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
)
Re: copy of object is a reference in PHP5
am 25.10.2007 20:05:21 von JustinCarmony
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:
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
Re: copy of object is a reference in PHP5
am 25.10.2007 20:34:48 von meltedown
On Oct 25, 2:05 pm, JustinCarmony wrote:
> 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:
>
>
> 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
bummer. What I have done is create objects with numerous properties
and a function to set 1 property and then return itself.
$label=new geocellobj();
$label->setalign('right');
$label->setvalign('middle');
$label->setstyle('border:0;');
array($label->settext('unique test'), $label->settext('different
text));
I don't think cloning will help me.
Re: copy of object is a reference in PHP5
am 26.10.2007 00:17:20 von dafox
groups2@reenie.org schrieb:
> On Oct 25, 2:05 pm, JustinCarmony wrote:
>> 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(...), ...)
Re: copy of object is a reference in PHP5
am 26.10.2007 06:58:20 von meltedown
On Oct 25, 6:17 pm, Thomas Hamacher wrote:
> grou...@reenie.org schrieb:
>
> > On Oct 25, 2:05 pm, JustinCarmony wrote:
> >> 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.