Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 2982

php-manual-en-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>References inside the constructor</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="index.html"><LINK
REL="UP"
TITLE="Classes and Objects"
HREF="language.oop.html"><LINK
REL="PREVIOUS"
TITLE="The magic functions __sleep and __wakeup"
HREF="language.oop.magic-functions.html"><LINK
REL="NEXT"
TITLE="References Explained"
HREF="language.references.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.oop.magic-functions.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 14. Classes and Objects</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.references.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.oop.newref"
></A
>References inside the constructor</H1
><P
>&#13;    Creating references within the constructor can lead to confusing
    results. This tutorial-like section helps you to avoid problems.
 
    <DIV
CLASS="informalexample"
><A
NAME="AEN5574"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>class Foo
{
    function Foo($name)
    {
        // create a reference inside the global array $globalref
        global $globalref;
        $globalref[] = &#38;$this;
        // set name to passed value
        $this-&#62;setName($name);
        // and put it out
        $this-&#62;echoName();
    }

    function echoName()
    {
        echo "&#60;br&#62;",$this-&#62;name;
    }
	
    function setName($name)
    {
        $this-&#62;name = $name;
    }
}</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
  </P
><P
>&#13;    Let us check out if there is a difference between
    <TT
CLASS="varname"
>$bar1</TT
> which has been created using
    the copy <TT
CLASS="literal"
>=</TT
> operator and
    <TT
CLASS="varname"
>$bar2</TT
> which has been created using
    the reference <TT
CLASS="literal"
>=&#38;</TT
> operator...

    <DIV
CLASS="informalexample"
><A
NAME="AEN5581"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>$bar1 = new Foo('set in constructor');
$bar1-&#62;echoName();
$globalref[0]-&#62;echoName();

/* output:
set in constructor
set in constructor
set in constructor */

$bar2 =&#38; new Foo('set in constructor');
$bar2-&#62;echoName();
$globalref[1]-&#62;echoName();

/* output:
set in constructor
set in constructor
set in constructor */</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    Apparently there is no difference, but in fact there is a
    very significant one: <TT
CLASS="varname"
>$bar1</TT
> and
    <TT
CLASS="varname"
>$globalref[0]</TT
> are _NOT_ referenced, they
    are NOT the same variable. This is because "new" does not
    return a reference by default, instead it returns a copy.
    <DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
      There is no performance loss (since PHP 4 and up use reference
      counting) returning copies instead of references. On the
      contrary it is most often better to simply work with copies
      instead of references, because creating references takes some
      time where creating copies virtually takes no time (unless none
      of them is a large array or object and one of them gets changed
      and the other(s) one(s) subsequently, then it would be wise to
      use references to change them all concurrently).
     </P
></BLOCKQUOTE
></DIV
>
    To prove what is written above let us watch the code below.

    <DIV
CLASS="informalexample"
><A
NAME="AEN5588"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>// now we will change the name. what do you expect?
// you could expect that both $bar1 and $globalref[0] change their names...
$bar1-&#62;setName('set from outside');

// as mentioned before this is not the case.
$bar1-&#62;echoName();
$globalref[0]-&#62;echoName();

/* output:
set from outside
set in constructor */

// let us see what is different with $bar2 and $globalref[1]
$bar2-&#62;setName('set from outside');

// luckily they are not only equal, they are the same variable
// thus $bar2-&#62;name and $globalref[1]-&#62;name are the same too
$bar2-&#62;echoName();
$globalref[1]-&#62;echoName();

/* output:
set from outside
set from outside */</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>   
   </P
><P
>&#13;   Another final example, try to understand it.
   
    <DIV
CLASS="informalexample"
><A
NAME="AEN5591"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>class A
{
    function A($i)
    {
        $this-&#62;value = $i;
        // try to figure out why we do not need a reference here
        $this-&#62;b = new B($this);
    }

    function createRef()
    {
        $this-&#62;c = new B($this);
    }

    function echoValue()
    {
        echo "&#60;br&#62;","class ",get_class($this),': ',$this-&#62;value;
    }
}


class B
{
    function B(&#38;$a)
    {
        $this-&#62;a = &#38;$a;
    }

    function echoValue()
    {
        echo "&#60;br&#62;","class ",get_class($this),': ',$this-&#62;a-&#62;value;
    }
}

// try to undestand why using a simple copy here would yield
// in an undesired result in the *-marked line
$a =&#38; new A(10);
$a-&#62;createRef();

$a-&#62;echoValue();
$a-&#62;b-&#62;echoValue();
$a-&#62;c-&#62;echoValue();

$a-&#62;value = 11;

$a-&#62;echoValue();
$a-&#62;b-&#62;echoValue(); // *
$a-&#62;c-&#62;echoValue();

/*
output:
class A: 10
class B: 10
class B: 10
class A: 11
class B: 11
class B: 11
*/</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.oop.magic-functions.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.references.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>The magic functions <TT
CLASS="literal"
>__sleep</TT
> and <TT
CLASS="literal"
>__wakeup</TT
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.oop.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>References Explained</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>