cvs: phpdoc /en/language references.xml

From: Date: Mon, 12 Nov 2007 22:54:49 +0000
Subject: cvs: phpdoc /en/language references.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-382@lists.php.net to get a copy of this message
felipe Mon Nov 12 22:54:49 2007 UTC Modified files: /phpdoc/en/language references.xml Log: Change example to Returning References (#42810) http://cvs.php.net/viewvc.cgi/phpdoc/en/language/references.xml?r1=1.49&r2=1.50&diff_format=u Index: phpdoc/en/language/references.xml diff -u phpdoc/en/language/references.xml:1.49 phpdoc/en/language/references.xml:1.50 --- phpdoc/en/language/references.xml:1.49 Wed Jun 20 22:24:12 2007 +++ phpdoc/en/language/references.xml Mon Nov 12 22:54:49 2007 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.49 $ --> +<!-- $Revision: 1.50 $ --> <chapter xml:id="language.references" xmlns="http://docbook.org/ns/docbook"> <title>References Explained</title> @@ -335,20 +335,24 @@ <programlisting role="php"> <![CDATA[ <?php -function &find_var($param) -{ - /* ...code... */ - return $found_var; +class foo { + public $value = 42; + + public function &getValue() { + return $this->value; + } } -$foo =& find_var($bar); -$foo->x = 2; -?> +$obj = new foo; +$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42. +$obj->value = 2; +echo $myValue; // prints the new value of $obj->value, i.e. 2. +?> ]]> </programlisting> </informalexample> In this example, the property of the object returned by the - <varname>find_var</varname> function would be set, not the + <varname>getValue</varname> function would be set, not the copy, as it would be without using reference syntax. </para> <note> @@ -357,13 +361,13 @@ <literal>&amp;</literal> in both places - to indicate that you return by-reference, not a copy as usual, and to indicate that reference binding, rather than usual assignment, should be done - for <varname>$foo</varname>. + for <varname>$myValue</varname>. </simpara> </note> <note> <simpara> If you try to return a reference from a function with the syntax: - <literal>return ($found_var);</literal> this will <emphasis>not</emphasis> + <literal>return ($this->value);</literal> this will <emphasis>not</emphasis> work as you are attempting to return the result of an <emphasis>expression</emphasis>, and not a variable, by reference. You can only return variables by reference from a function - nothing else.

« previous php.doc.cvs (#382) next »