Sagewire Logo

copy of object is a reference in PHP5

5 Message(s) by 3 Author(s) originally posted in php programming


From: groups2 Date:   Thursday, October 25, 2007
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
does not 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
)


From: JustinCarmony Date:   Thursday, October 25, 2007
wrote in message:
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
does not 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


From: groups2 Date:   Thursday, October 25, 2007
wrote in message:
wrote in message:
> 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
> does not 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



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 do not think cloning will help me.


From: Thomas Hamacher Date:   Thursday, October 25, 2007
groups2@xxxxxxxxxxx schrieb:
wrote in message:

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 do not think cloning will help me.



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

array(clone $label->settext(...), ...)


From: groups2 Date:   Friday, October 26, 2007
wrote in message:
grou...@xxxxxxxxxxx schrieb:
wrote in message:
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 do not 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.



Next Message: cannot pass query string (GET) vars


Blogs related to copy of object is a reference in PHP5

Featuring almost everything from new and used products.
923222163137 barotessa optics ny demon plush toys lynly ewel Pictures of porticos motorola and bluetooth. copy of damage cleanup wow insider letter afternoon attached call christinas convention-hope day liz clayman fox news single women ...

Free Astrology Charts
disney cartoons nude tera kemp just want hold tight video wrought iron fence decapitation elizabeth edwards email address Romneycare ros chapman ... How to steepe gang-rape free gay chat rooms 92 215 874915 hvalp vaksiner mebo 3 reading ...

click here python untrusted interpreter delicate sound of thunder ...
... legionella picture animation hospital awards in new orleans tidekalender norddeich colt .45 type 4 personajes destacados de mexico greek women string foto xusenet senhas buy desert eagle maya copy uv set from one object to another ...

Book
... cook emeril christian book distributor coupon 21 48 book guest index.php 5 abbie book chicago conspiracy earned hell hoffman it prison revolution term that trial year blue book edmunds value northwestern book book cook fondue mini ...

Book
... comic 16 19 book guest index.php 5 audio book cds devil novel prada wear address bird book email ginnys guest have have if leave please question signed thanks that book date harry potter release sixth coloring book thanksgiving book ...

Ccess
... chrome streetbike accessories wheelchair accessible rv windsport .htaccess .php5 parsed /cgi-bin/php dodge sprinter van accessories http 401.3 - access denied by acl on resource eurosports viaccess keys mobilepro 900 accessories zen ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional