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.
What is the difference between new and &new
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
'&' references the object that you are creating, it doesn't actually use the object.
More on reference from PHP.net...
More on reference from PHP.net...
Re: What is the difference between new and &new
That's not legal syntax under PHP 5.ZeroFear wrote:$object = &new Object();
- 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
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.
I believe the '&' is ignored and handles, not references, are used.bokehman wrote:That's not legal syntax under PHP 5.
(#10850)
Re: What is the difference between new and &new
It raises an E_STRICT.arborint wrote:I believe the '&' is ignored and handles, not references, are used.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Doesn't seem to be the case for me...
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.
Code: Select all
<?php
error_reporting(E_STRICT);
class A {}
$a =& new A;
?>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.
E_STRICT is raised when you pass an object by reference, but not assign, because in php5 objects are always passed by reference.
However, I think, like you say, they may have removed that particular E_STRICT message because it is/was such common practice.
Code: Select all
<?php
function & foo (&$obj)
{
return $obj;
}
$obj =& foo(new stdClass);
?>- 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
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.bokehman wrote:It raises an E_STRICT.
(#10850)