problema del objeto de clases php

<?php

$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);
?> 

what is the difference between below given 2 lines?

$assigned   =  $instance;
$reference  =& $instance; 

as in OOP object is by default assign by reference . so $assigned will also have & $instance.

output of above code is

NULL
NULL
object(SimpleClass)#1 (1) {
   ["var"]=>
     string(30) "$assigned will have this value"
}

preguntado el 31 de enero de 12 a las 08:01

2 Respuestas

as in OOP object is by default assign by reference

This is not totally true.

Que $instance holds is the value of the object id, when you assign an object to another variable, you only passed the object id.

Así que cuando lo hagas $assigned = $instance;, you are passing the object id which $instance holds to the variable $assigned, Y $instance = null is only set the variable $instance to null, nothing will affect to $assigned.

Pero cuando lo haces $reference =& $instance, you are creating a reference to the variable $instance, así que si pones $instance anular, $reference will also be null.

Respondido el 31 de enero de 12 a las 13:01

"so $assigned will also have & $instance"

Close. $assigned will become a reference that references the same thing that $instance references at the moment of assignment.

En otras palabras:

<?php

$instance = new SimpleClass();


$assigned   =  $instance; //$assigned now references SimpleClass instance (NOT $instance)
//Other than both pointing the same thing, the two variables are not related at all (sort of... :p)


$reference  =& $instance;

//$reference does not point at the SimpleClass instance like $assigned does, but rather it points at $instance which points at the SimpleClass instance. It is, in a sort of incorrect way, a reference to the reference.

//$instance still references the SimpleClass instance here, so it does what you'd expect
$instance->var = '$assigned will have this value';

$instance = null; // $instance and $reference become null

//They both become null because $reference references $instance. In the same way that:

$a = 5;
$b =& $a;

$a = 3; //$b now equals 3.

//Since the reference held in $instance is wiped, $reference's value is wiped too (since it points to that same reference

That's a bit of a convoluted explanation, I'm afraid, but hopefully it gets the point across. The main point is this: variables don't store objects; variables reference objects. Objects are not copied by default, but rather, the reference is copied.

$a = new ...; $b = $a;

That will copy the reference, not the object. $b = clone $a; would copy the object.

If you're familiar with Java, it's sort of like how in Java, objects are passed to methods by reference, but the references are passed by value (the reference is copied).

The $assigned copies the reference, and the $reference references the variable that references the object.

Respondido el 31 de enero de 12 a las 13:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.