Document variable properties.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@288221 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Torben Wilson 2009-09-10 05:45:54 +00:00
parent 459f5061ab
commit 19cbd580fd
2 changed files with 67 additions and 2 deletions

View file

@ -646,6 +646,34 @@ echo "I'd like to have another {${ strrev('reeb') }}, hips";
-->
</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>

View file

@ -721,9 +721,46 @@ echo "$a $hello";
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