Re: PHP 5 confusion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Thus wrote Don:
> Hi,
>  
> Reading the PHP 5 documentation at: HYPERLINK
> "http://www.php.net/manual/en/language.oop5.basic.php"http://www.php.net/man
> ual/en/language.oop5.basic.php, I am confused.
  
After looking at the example output, it is not correct, the real
output should be:

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

> In the example given, what is the difference between:
> $assigned  =  $instance;
> $reference  =& $instance;
>  
> I would expect all of the var_dump to display NULL

The manual needs some changes, the example 19-3 is assuming that
both 19-1 and 19-2 exist in the code, which should result with what
I have above.

>  
> The doc says "When assigning an already created instance of an object to a
> new variable, the new variable will access the same instance as the object
> that was assigned." so the above assignments seem the same to me and setting
> $instance to NULL should also set $assigned to NULL.
>  
> If this is not the case and not using the '&' specifies a 'copy'
> (contradicting the documentation) then what's the purpose of object cloning?
>  
> I tried the code below and find that it gives the exact same output
> regardless if I am using the '&' or not so it seems to assign be reference
> either way.

The way objects are assigned in php5 are a bit different than in
php4, basically in php4 if you do something like:

$var1 = new Object();
$var2 = $var1;

It is treated like a clone, where now there are two objects.

php5 on the other hand, its a little more complex. When an object
is create in php5 and assigned to a variable, there are two things
that happen:

  $var1 = new Object();

  1. an object is created
  2. a variable, referencing that object, is created.


When you assign a variable (that references the object) to another
variable, we now have two variables referencing the object:

  $var2 = $var1;

So if I issue:

  unset($var1);

the same object that was originally crated still exists in $var2,
until i destroy $var2, then that object gets destroyed as well.


Now, with the = & assignment:

  $var2 = &$var1;

What happens here is that both $var1 and &var2 become in a way
identical.  If I issue:
  
  unset($var1);

Then both $var1 and $var2 become unset, thus the original object
no longer is being referenced by anything.


I hope that didn't complicate things, but helped clarified what you
were looking at.


Curt
-- 
Quoth the Raven, "Nevermore."

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux