Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old April 12th, 2007, 03:55 PM
lister
Guest
 
Posts: n/a
Default Arrays always use references?

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

  #2  
Old April 12th, 2007, 06:55 PM
Alvaro G. Vicario
Guest
 
Posts: n/a
Default 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
--
  #3  
Old April 12th, 2007, 09:55 PM
lister
Guest
 
Posts: n/a
Default 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>

  #4  
Old April 12th, 2007, 11:45 PM
Henk verhoeven
Guest
 
Posts: n/a
Default 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?
>
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles