PHP Objects & References

Posted on 2007-10-12
Last Modified: 2013-12-13

I have 2 quick questions about references in PHP, firstly, if i did the following:

     $this->object = new Object();
     $var1 = $this->object;

Would $var1 contain a copy of $this->object, and hence clone it?

Secondly, If I did the following:

     $ref =& $this->object;
     $secondref = $ref;

Does that mean $secondref is a reference to $this->object ($secondref is set to the same memory location as $ref is set to)? Or does PHP treat $ref as the same as $this->object and hence would not contain a reference to it and possibly duplicate it?

Thanks for your help,

Question by:Xavior2K3
    LVL 48

    Expert Comment

    1) depends on you php version in 4 you get a copy in 5 it's the same object

    2) $ref is becoming an "alias" for $this->object, but again in php 4 you get a copy, in php5 it's the same object.

    See for difference between 4 and 5 in the object model.
    LVL 1

    Author Comment

    Sorry, I forgot to mention I'm using PHP 5.

    So does this mean these are equivalent?:

         $var1 = $this->object;
         $var1 =& $this->object;

    And that after these statements:

         $ref =& $this->object;
         $secondref = $ref;

    $this->object, $ref and $secondref all point the the same object?
    LVL 48

    Accepted Solution

    For your case yes, but the references work a little bit different e.g.
    $var1 =& $this->object;
    $this->object = new Object();
    $var1 == $this->object

    in this case $ref and $this->object contain the same object, but
    $var1 = $this->object;
    $this->object = new Object();
    $var1 != $this->object

    So working with objects in php5 there is no need to use the &. It causes you more trouble than you can imagine. See

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now