is the "&" necessary? $guestbook =& new Gu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HEMOglobina
Forum Newbie
Posts: 6
Joined: Tue Aug 09, 2005 7:23 pm

is the "&" necessary? $guestbook =& new Gu

Post by HEMOglobina »

While following a tutorial for smarty (http://smarty.php.net/sampleapp/sampleapp_p2.php) I found the following line of code:

Code: Select all

$guestbook =& new Guestbook;
What does the "&" means? Is it needed? I run the code without it ($guestbook = new Guestbook;) and the application behaved just the same...

Thanks a lot,
HEMOglobina
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

it is called reference, it is used when you are refering the object and do not want to create a new copy of the original. This is one automatically in php5.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

...continuing the thought...

However, I find it a good practice to continue doing "=&" as it's more explicit what's happening, even if the language does it implicitly.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

so how do you re-access your value. i really mean is can you give a good example when you would want to use this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you access it like normal.. The nice part is, you change it to (almost) anything, and the original changes too.
Post Reply