What is the point of references in a new statement?

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
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

What is the point of references in a new statement?

Post by LonelyProgrammer »

Example:

Code: Select all

class foobar() { ... }

$aFooBar = & new foobar();
How is it different from:

Code: Select all

$aFooBAr = new foobar();
Read the docs on references...I still feel...kind of lost.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

With the & you get a reference to the object, without it you get a copy. The question really is, why would you want a copy ? (btw, PHP5 will automatically give you a reference without the & )
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Copy of the object...?

Post by LonelyProgrammer »

Wait a sec, I still don't get it. If I want a new instance of class, why would I want a reference to the class? Unless if you meant by copy, you are saying that PHP creates two copies whenver I use "$c = new classC();", one which is $c, and another one which is "new classC()" ?
Post Reply