note 79856 added to language.oop5.basic
| From: | aaron at thatone dot com | Date: | Sun, 16 Dec 2007 02:46:09 +0000 |
| Subject: | note 79856 added to language.oop5.basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133626@lists.php.net to get a copy of this message | ||
I was confused at first about object assignment, because it's not quite the same as normal
assignment or assignment by reference. But I think I've figured out what's going on.
First, think of variables in PHP as data slots. Each one is a name that points to a data slot that
can hold a value that is one of the basic data types: a number, a string, a boolean, etc. When you
create a reference, you are making a second name that points at the same data slot. When you assign
one variable to another, you are copying the contents of one data slot to another data slot.
Now, the trick is that object instances are not like the basic data types. They cannot be held in
the data slots directly. Instead, an object's "handle" goes in the data slot. This is
an identifier that points at one particular instance of an obect. So, the object handle, although
not directly visible to the programmer, is one of the basic datatypes.
What makes this tricky is that when you take a variable which holds an object handle, and you assign
it to another variable, that other variable gets a copy of the same object handle. This means that
both variables can change the state of the same object instance. But they are not references, so if
one of the variables is assigned a new value, it does not affect the other variable.
<?php
// Assignment of an object
Class Object{
public $foo="bar";
};
$objectVar = new Object();
$reference =& $objectVar;
$assignment = $objectVar
//
// $objectVar --->+---------+
// |(handle1)----+
// $reference --->+---------+ |
// |
// +---------+ |
// $assignment -->|(handle1)----+
// +---------+ |
// |
// v
// Object(1):foo="bar"
//
?>
$assignment has a different data slot from $objectVar, but its data slot holds a handle to the same
object. This makes it behave in some ways like a reference. If you use the variable $objectVar to
change the state of the Object instance, those changes also show up under $assignment, because it is
pointing at that same Object instance.
<?php
$objectVar->foo = "qux";
print_r( $objectVar );
print_r( $reference );
print_r( $assignment );
//
// $objectVar --->+---------+
// |(handle1)----+
// $reference --->+---------+ |
// |
// +---------+ |
// $assignment -->|(handle1)----+
// +---------+ |
// |
// v
// Object(1):foo="qux"
//
?>
But it is not exactly the same as a reference. If you null out $objectVar, you replace the handle in
its data slot with NULL. This means that $reference, which points at the same data slot, will also
be NULL. But $assignment, which is a different data slot, will still hold its copy of the handle to
the Object instance, so it will not be NULL.
<?php
$objectVar = null;
print_r($objectVar);
print_r($reference);
print_r($assignment);
//
// $objectVar --->+---------+
// | NULL |
// $reference --->+---------+
//
// +---------+
// $assignment -->|(handle1)----+
// +---------+ |
// |
// v
// Object(1):foo="qux"
?>
----
Server IP: 64.71.164.2
Probable Submitter: 76.200.128.249
----
Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php
Edit -- https://master.php.net/note/edit/79856
Del: integrated -- https://master.php.net/note/delete/79856/integrated
Del: useless -- https://master.php.net/note/delete/79856/useless
Del: bad code -- https://master.php.net/note/delete/79856/bad+code
Del: spam -- https://master.php.net/note/delete/79856/spam
Del: non-english -- https://master.php.net/note/delete/79856/non-english
Del: in docs -- https://master.php.net/note/delete/79856/in+docs
Del: other reasons-- https://master.php.net/note/delete/79856
Reject -- https://master.php.net/note/reject/79856
Search -- https://master.php.net/manage/user-notes.php