References and new Objects

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
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

References and new Objects

Post 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.
Post Reply