Page 1 of 1

References and new Objects

Posted: Fri Apr 04, 2003 1:59 pm
by EricS
On PHP.net I was going over references again and I couldn't find anywhere that talked about using references on new objects. Example:

Code: Select all

$newObject =& new objectClass();
Okay, so rather than creating a new object and getting a copy of the object, I'm creating a new object and getting a reference to that object. I understand that. My question is, should I have an ampersand infront of the constructor for the class in order to be using this correctly? Which of the following code is correct, and if they are both correct, which is considered better programming.

Example 1:

Code: Select all

class objectClass()
{
    function &objectClass()
    {
        blah......
    }
}
$newObject =& new objectClass();
Example 2:

Code: Select all

class objectClass()
{
    function objectClass()
    {
        blah......
    }
}
$newObject =& new objectClass();
The only difference between the two statements is the constructor definition.

Any input would be helpful.