1

Below is the text from PHP Manual :

PHP treats objects in the same way as references or handles, meaning that each variable contains an object reference rather than a copy of the entire object.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

After going through the above text I've following doubts in my mind:

  1. What exactly the 'Object' is?
  2. What exactly the 'Object Reference' is?
  3. What exactly an 'Object Identifier' is?
  4. Does the entity called 'Object Identifier' work implicitly/internally?
  5. How to create a copy of the 'Object Identifier'?
  6. Do the terms 'Object Reference' and 'Object Identifier' mean the same thing?
  7. What exactly the 'Object Accessors' are?
  8. Do the terms 'Instance of a class', 'Instance of an object', 'Object variable', 'Class instance', 'Object instance' mean the same thing? If yes, what these entities indicate? If no, what are the differences in between their meanings?

How all of the above entities function?

Can someone please clear the doubts I have in an easy to understand, simple and lucid language in short and step-by-step manner?

It would be great if someone can explain these concepts with some suitable, working code example having explanatory comments at appropriate places in the code.

If possible, someone can also explain with the help of pictorial representation of working of these concepts. It would be highly appreciated.

You can take up following example or specify your own suitable example to explain all of the above concepts.

<?php
  class A {
    public $foo = 1;
  }  

  $a = new A;
  $b = $a;     

  $b->foo = 2;
  echo $a->foo."\n";


  $c = new A;
  $d = &$c;    


  $d->foo = 2;
  echo $c->foo."\n";


  $e = new A;

  function foo($obj) {
    $obj->foo = 2;
  }

  foo($e);
  echo $e->foo."\n";    
?>

Thank You.

Reference links from the PHP Manual :

  1. https://secure.php.net/manual/en/oop5.intro.php#oop5.intro
  2. https://secure.php.net/manual/en/language.oop5.references.php#language.oop5.references
PHPLover
  • 1
  • 51
  • 158
  • 311

1 Answers1

2
  1. An Object is an instance of a class that you create with new Classname.
  2. An Object Reference is a PHP variable whose value is an Object.
  3. An Object Identifier is a value internal to the PHP implementation that identifies a particular object. It's probably something like an index into an array that contains pointers to all the objects.
  4. Yes, they're internal, not visible to PHP applications.
  5. Assign a variable containing an object to another variable. Internally, both variables contain copies of the object identifier.
  6. They're not the same thing, but closely related. An object reference is a PHP variable, the object identifier is the internal data that it contains.
  7. Object accessors are operations that access the contents of objects, such as $d->foo.
  8. Yes, they're all essentially synonyms.

This is all just implementation details for how you can have multiple variables referring to the same object without having to use reference variables explicitly.

$a = new A;
$b = $a;
$a->foo = 2;
echo $b->foo; // echoes 2

The assignment isn't making a copy of the object, it's just copying the object identifier. All copies of an object identifier refer to the same object. Think of it like a pointer in languages like C or C++ -- when you copy a pointer, both variables refer to the same memory.

To make a copy of an object, you have to use clone.

$c = clone $a;

This is different from how arrays work, which is that assignment makes a copy, unless you use an explicit reference variable. (As an optimization, PHP uses copy-on-write, so it doesn't actually copy the array memory unless you modify it.)

$x = array(1);
$y = $x;
$z = &$x;
$x[0] = 2;
echo $y[0]; // echoes 1
echo $z[0]; // echoes 2
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • @ Barmar : Thanks for your answer. Can you please explain these concepts practically(in actual use) with the help of code example I've given or some other suitable working code example? If possible, pictorial representation of actual working of these entities will be much more helpful to me in understanding these concepts better. Thanks once again. I'm keenly waiting for your reply. – PHPLover Nov 23 '17 at 03:00
  • This is all just implementation details, I'm not sure what you're looking for as practical demonstrations. – Barmar Nov 23 '17 at 03:03
  • I actually want to know the working details of all of these entities in the code example I've mentioned in my question. You can add comments to this code to explain the concepts or you can add some other suitable working code example with comments explaining the above concepts. So, that it will make the things crystal clear. With your current answer I got to understand the concepts theoretically not practically. So, I'm requesting for practical demonstration of working of these entities. Pictorial representation of working would be most useful aid though. Thank You. – PHPLover Nov 23 '17 at 03:09
  • I don't really know the low-level details, just the concepts. I don't see how any of this makes any practical difference. – Barmar Nov 23 '17 at 03:10
  • If you're familiar with languages like C++, an object identifier could be thought of as a pointer to a C++ object. – Barmar Nov 23 '17 at 03:12
  • Do all the entities mentioned in eighth question contain 'Object Reference' only? – PHPLover Nov 23 '17 at 03:35
  • You are saying in second point that "An Object Reference is a PHP variable whose value is an Object". So, my doubt is does the term 'Object Reference' mean the same thing as the terms specified in the eighth question? – PHPLover Nov 23 '17 at 03:35
  • The object reference is the variable, the instance is the object that it refers to. – Barmar Nov 23 '17 at 03:38
  • In my example, `$a` and `$b` are two object references to the same object instance. – Barmar Nov 23 '17 at 03:39