What is the difference between new and &new

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
ZeroFear
Forum Newbie
Posts: 14
Joined: Tue Feb 14, 2006 10:47 pm

What is the difference between new and &new

Post by ZeroFear »

what is the difference between these two statements?

$object = new Object();

$object = &new Object();



Anyone? I have a few guesses but id like to be sure.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

'&' references the object that you are creating, it doesn't actually use the object.

More on reference from PHP.net...
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Re: What is the difference between new and &new

Post by bokehman »

ZeroFear wrote:$object = &new Object();
That's not legal syntax under PHP 5.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What is the difference between new and &new

Post by Christopher »

The problem in PHP4 was that the "$obj = new MyClass();" allocated two object -- one was created by the "new" and the assignment made a copy. Assigning by reference did not make the copy so became common usage.
bokehman wrote:That's not legal syntax under PHP 5.
I believe the '&' is ignored and handles, not references, are used.
(#10850)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Re: What is the difference between new and &new

Post by bokehman »

arborint wrote:I believe the '&' is ignored and handles, not references, are used.
It raises an E_STRICT.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Doesn't seem to be the case for me...

Code: Select all

<?php

error_reporting(E_STRICT);

class A {}
$a =& new A;

?>
Running PHP 5.1.4. They did some weird stuff with E_STRICT though.

As for whether or not the reference is ignored, it's hard to say, because unless you have a variable, handles and references are identical.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

E_STRICT is raised when you pass an object by reference, but not assign, because in php5 objects are always passed by reference.

Code: Select all

<?php

function & foo (&$obj)
{
    return $obj;
}

$obj =& foo(new stdClass);

?>
However, I think, like you say, they may have removed that particular E_STRICT message because it is/was such common practice.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What is the difference between new and &new

Post by Christopher »

bokehman wrote:It raises an E_STRICT.
Yes ... which can be helpful in identifying in identifying the problem when upgrading from PHP4 to PHP5. But I believe internally that they ignore attempts to assign an object by reference and just use handles -- they obviously identify the circumstance.
(#10850)
Post Reply