Assgined by reference , value of object?
I am learning OOP's PHP by reading the php offical documents
first:
class SimpleClass
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
Second:
$instance = new SimpleClass();
$assigned = $instance;
$reference =& $instance;
$instance->var = '$assigned will have this value';
$instance = null; // $instance and $reference become null
var_dump($instance);
var_dump($reference);
var_dump($assigned);
Result:
NULL
NULL
object(SimpleClass)#1 (1) {
["var"]=>
string(30) "$assigned will have this value"
}
This example I dont 开发者_如何转开发understand about assgined by reference and value
here .even I see the result output.But I still not get an idea.
My question is why $instance and $reference become null?
My question is why
$instance
and$reference
become null?
PHP references operate in a funny way. I'm going to break out a real-life analogy.
Imagine you have a cup. The cup is full of water.
The cup is a variable. The water is the contents of the variable.
Any references you create point at the cup, not the water.
If you replace the water in the cup with coffee, then all of the references to that cup suddenly contain the same coffee.
And when you empty the cup out, all of the references to the cup are suddenly empty.
Now, objects are quite fun. You see, when you assign them to variables, it actually does so kind of by reference. But objects-by-ref aren't normal references. From the PHP interactive prompt:
php > class Foo { public $bar; }
php > $f = new Foo();
php > $f->bar = 1;
php > $f_copy = $f;
php > $f_copy->bar = 2;
php > echo $f->bar;
2
Not what you expected, right? It gets funnier.
php > $f_copy = $f;
php > $copy_ref = &$f_copy;
php > $copy_ref->bar = 3;
php > echo $f->bar;
3
That is a bit more expected. But is this?
php > $f_copy = null;
php > print_r($copy_ref);
php > var_export($copy_ref);
NULL
php > print_r($f);
Foo Object
(
[bar] => 3
)
As you can see, even though we completely emptied out the variable containing the reference to our object, the original object was untouched, even though that object was placed into the copy by reference!
Welcome to PHP, where things like this are normal. I suggest frequent checks of your own sanity.
Edit: Oh, one more thing. If you need to make a real copy of the object, you have to clone
it:
php > $not_f = clone $f;
php > $not_f->bar = 'I am not $f';
php > echo $f->bar;
3
php > echo $not_f->bar;
I am not $f
It also works on refs:
php > $not_f_copy = $not_f;
php > $not_copy_ref = &$not_f_copy;
php > $headsplode = clone $not_copy_ref;
php > $not_f->bar = 'No, I am Spartacus!';
php > echo $headsplode->bar;
I am not $f
Oh, and before someone asks: In some languages, references work at the value level, not the variable level. Perl is a good example here. If you create a reference to a variable, then reassign the original, the reference still contains the original value. I came to PHP through Perl, and this seemingly small difference drove me up the wall.
$instance
and $reference
both refer to the same memory location.
If you will, $instance
is merely the first name that was given to that memory location, and $reference
is the second name. The act of "assigning by reference" can be described as "making a memory location known by an additional name".
If you now set that memory location to NULL
, all names that refer to that memory location will yield null.
However, $assigned
is a (shallow) copy of the original object. Therefore it is in a different location. And that's why it still has the $var
value.
Related PHP documentation:
- What References Are Not
- Object Cloning
value of a variable is the actual content of that variable. for example, if you have an byte array of 100 elements, then the value of this array takes 100 bytes in memory.
reference to a variable is different, it is the memory address of that variable. for example, on x86 sytems, this reference should take 4 bytes, and on 64bit system, it takes 8 bytes.
the above 2 points address the common sense of value & reference (pointer) in OOP; PHP might save these another different way
$instance becomes null because you set it to null, and so, the reference to a null is, of course, null.
and about the variable $assigned, when php execute to this line of code
$assigned = $instance;
php creates another object and copy the values in $instance to $assigned, that's why when you call var_dump the third time, it shows you the original values inside $instance, although $instance has already been set to null bebore the var_dump call.
精彩评论