svn: /phpdoc/en/trunk/language/ types/string.xml variables.xml
| From: | Torben Wilson | Date: | Thu, 10 Sep 2009 05:45:54 +0000 |
| Subject: | svn: /phpdoc/en/trunk/language/ types/string.xml variables.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-4891@lists.php.net to get a copy of this message | ||
torben Thu, 10 Sep 2009 05:45:54 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=288221
Log:
Document variable properties.
Changed paths:
U phpdoc/en/trunk/language/types/string.xml
U phpdoc/en/trunk/language/variables.xml
Modified: phpdoc/en/trunk/language/types/string.xml
===================================================================
--- phpdoc/en/trunk/language/types/string.xml 2009-09-10 05:15:24 UTC (rev 288220)
+++ phpdoc/en/trunk/language/types/string.xml 2009-09-10 05:45:54 UTC (rev 288221)
@@ -646,6 +646,34 @@
-->
</programlisting>
</informalexample>
+
+ <para>
+ It is also possible to access class properties using variables
+ within strings using this syntax.
+ </para>
+
+ <informalexample>
+ <programlisting role="php">
+<![CDATA[
+<?php
+class foo {
+ var $bar = 'I am bar.';
+}
+
+$foo = new foo();
+$bar = 'bar';
+$baz = array('foo', 'bar', 'baz', 'quux');
+echo "{$foo->$bar}\n";
+echo "{$foo->$baz[1]}\n";
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+I am bar.
+I am bar.
+ </screen>
+ </informalexample>
<note>
<para>
Modified: phpdoc/en/trunk/language/variables.xml
===================================================================
--- phpdoc/en/trunk/language/variables.xml 2009-09-10 05:15:24 UTC (rev 288220)
+++ phpdoc/en/trunk/language/variables.xml 2009-09-10 05:45:54 UTC (rev 288221)
@@ -721,9 +721,46 @@
wanted <varname>$$a</varname> as the variable and then the [1]
index from that variable. The syntax for resolving this ambiguity
is: <varname>${$a[1]}</varname> for the first case and
- <varname>${$a}[1]</varname> for the second.
+ <varname>${$a}[1]</varname> for the second.
</simpara>
-
+
+ <simpara>
+ Class properties may also be accessed using variable property
+ names. The variable property name will be resolved within the
+ scope from which the call is made. For instance, if you have an
+ expression such as <varname>$foo->$bar</varname>, then the local
+ scope will be examined for <varname>$bar</varname> and its value
+ will be used as the name of the property
+ of <varname>$foo</varname>. This is also true
+ if <varname>$bar</varname> is an array access.
+ </simpara>
+
+ <para>
+ <example>
+ <title>Variable function example</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+class foo {
+ var $bar = 'I am bar.';
+}
+
+$foo = new foo();
+$bar = 'bar';
+$baz = array('foo', 'bar', 'baz', 'quux');
+echo $foo->$bar . "\n";
+echo $foo->$baz[1] . "\n";
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+I am bar.
+I am bar.
+ </screen>
+ </example>
+ </para>
+
<warning>
<simpara>
Please note that variable variables cannot be used with PHP's