git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@43100 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
André Langhorst 2001-03-09 22:29:02 +00:00
parent 7c4ccc09bd
commit 0425348f09

View file

@ -209,7 +209,7 @@ class foo {
function foo($name) {
// create a reference inside the global array $globalref
global $globalref;
$globalref[] = &$this;
$globalref[] = &$this;
// set name to passed value
$this->setName($name);
// and put it out
@ -217,7 +217,7 @@ class foo {
}
function echoName() {
echo "<br>",$this->Name;
echo "&lt;br&gt;",$this->Name;
}
function setName($name) {
@ -226,11 +226,12 @@ class foo {
}
</programlisting>
</informalexample>
</para>
<para>
Let us check out if there is a difference between <varname>$bar1</varname> which has been created using the copy <literal>=</literal> operator
and <varname>$bar2</varname> which has been created using the reference <literal>=&</literal> operator...
</para>
and <varname>$bar2</varname> which has been created using the reference <literal>=&amp;</literal> operator...
<informalexample>
<programlisting role="php">
@ -244,7 +245,7 @@ class foo {
set in constructor
set in constructor */
$bar2 =& new foo('set in constructor');
$bar2 =&amp; new foo('set in constructor');
$bar2->echoName();
$globalref[1]->echoName();
@ -255,7 +256,7 @@ class foo {
</programlisting>
</informalexample>
</para>
<para>
Apparently there is no difference, but in fact there is a very significant one:
<varname>$bar1</varname> and <varname>$globalref[0]</varname> are _NOT_ referenced, they are NOT the same variable.
@ -270,7 +271,7 @@ class foo {
</simpara>
</note>
To prove what is written above let us watch the code below.
</para>
<informalexample>
<programlisting role="php">
@ -300,7 +301,7 @@ class foo {
</programlisting>
</informalexample>
</para>
<para>
Another final example, try to understand it.
@ -319,26 +320,26 @@ class a {
}
function echoValue() {
echo "<br>","class ",get_class($this),': ',$this->value;
echo "&lt;br&gt;","class ",get_class($this),': ',$this->value;
}
}
class b {
function b(&$a) {
$this->a = &$a;
function b(&amp;$a) {
$this->a = &amp;$a;
}
function echoValue() {
echo "<br>","class ",get_class($this),': ',$this->a->value;
echo "&lt;br&gt;","class ",get_class($this),': ',$this->a->value;
}
}
// try to undestand why using a simple copy here would yield
// in an undesired result in the *-marked line
$a =& new a(10);
$a =&amp; new a(10);
$a->createRef();
$a->echoValue();
@ -361,8 +362,8 @@ class b: 11
class b: 11
*/
</programlisting>
</informalexample>
</para>
</informalexample>
</para>
</sect1>