You are here: Re: [PHP] PHP 5 confusion « PHP « IT news, forums, messages
Re: [PHP] PHP 5 confusion

Posted by Curt Zirzow on 01/08/05 21:12

* 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."

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация