Connecting Tech Pros Worldwide Help | Site Map

Arrays always use references?

lister
Guest
 
Posts: n/a
#1: Apr 12 '07
I have the following code:

$Foo = new CFoo();
$Foo->Bar = 5;

$_SESSION['foo'] = $Foo;
$_SESSION['bar'] = $Foo->Bar;

$Foo->Bar = 10;

echo $Foo->Bar;
echo $_SESSION['foo']->Bar;
echo $_SESSION['bar'];

outputs
10
10
10

How can I force an array to contain an instance rather than a
reference, so for instance $_SESSION['bar'] is still equal to 5?

Thanks

Alvaro G. Vicario
Guest
 
Posts: n/a
#2: Apr 12 '07

re: Arrays always use references?


*** lister escribió/wrote (12 Apr 2007 07:53:54 -0700):
Quote:
$Foo = new CFoo();
$Foo->Bar = 5;
>
$_SESSION['foo'] = $Foo;
$_SESSION['bar'] = $Foo->Bar;
>
$Foo->Bar = 10;
>
echo $Foo->Bar;
echo $_SESSION['foo']->Bar;
echo $_SESSION['bar'];
>
outputs
10
10
10
In my system, this code prints:

10
10
5

A wild guess... register_globals set to on?

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
lister
Guest
 
Posts: n/a
#3: Apr 12 '07

re: Arrays always use references?


On Apr 12, 6:53 pm, "Alvaro G. Vicario"
<webmas...@NOSPAMdemogracia.comwrote:
Quote:
*** lister escribió/wrote (12 Apr 2007 07:53:54 -0700):
>
>
>
>
>
Quote:
$Foo = new CFoo();
$Foo->Bar = 5;
>
Quote:
$_SESSION['foo'] = $Foo;
$_SESSION['bar'] = $Foo->Bar;
>
Quote:
$Foo->Bar = 10;
>
Quote:
echo $Foo->Bar;
echo $_SESSION['foo']->Bar;
echo $_SESSION['bar'];
>
Quote:
outputs
10
10
10
>
In my system, this code prints:
>
10
10
5
>
A wild guess... register_globals set to on?
>
--
-+http://alvaro.es- Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web:http://bits.demogracia.com
+- Mi web de humor con rayos UVA:http://www.demogracia.com
--- Hide quoted text -
>
- Show quoted text -
Many thanks for your reply, but I think I've just realised what is
going on.
Bar is actually another object in my real code, and I've just read
that all objects are references. D'oh.
I have also found the "clone" command. <slinks away>

Henk verhoeven
Guest
 
Posts: n/a
#4: Apr 12 '07

re: Arrays always use references?


Maybe you are using php4 where lister uses php5?

Alvaro G. Vicario schreef:
Quote:
*** lister escribió/wrote (12 Apr 2007 07:53:54 -0700):
Quote:
>$Foo = new CFoo();
>$Foo->Bar = 5;
>>
>$_SESSION['foo'] = $Foo;
>$_SESSION['bar'] = $Foo->Bar;
>>
>$Foo->Bar = 10;
>>
>echo $Foo->Bar;
>echo $_SESSION['foo']->Bar;
>echo $_SESSION['bar'];
>>
>outputs
>10
>10
>10
>
In my system, this code prints:
>
10
10
5
>
A wild guess... register_globals set to on?
>
Closed Thread