aidan Mon Oct 11 02:27:54 2004 EDT
Modified files: /phpdoc/en/language/oop5 magic.xml Log: Clarified when __toString is called (bug #30379) http://cvs.php.net/diff.php/phpdoc/en/language/oop5/magic.xml?r1=1.4&r2=1.5&ty=u Index: phpdoc/en/language/oop5/magic.xml diff -u phpdoc/en/language/oop5/magic.xml:1.4 phpdoc/en/language/oop5/magic.xml:1.5 --- phpdoc/en/language/oop5/magic.xml:1.4 Thu Oct 7 10:04:17 2004 +++ phpdoc/en/language/oop5/magic.xml Mon Oct 11 02:27:51 2004 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.4 $ --> +<!-- $Revision: 1.5 $ --> <sect1 id="language.oop5.magic"> <title>Magic Methods</title> <para> @@ -64,10 +64,12 @@ The <literal>__toString</literal> method allows a class to decide how it will react when it is converted to a string. </para> - <programlisting role="php"> + <example> + <title>Simple example</title> + <programlisting role="php"> <![CDATA[ <?php -// Define a simple class +// Declare a simple class class TestClass { public $foo; @@ -85,13 +87,39 @@ echo $class; ?> ]]> - </programlisting> - &example.outputs; - <screen> + </programlisting> + &example.outputs; + <screen> <![CDATA[ Hello ]]> - </screen> + </screen> + </example> + <para> + It is worth noting that the <literal>__toString</literal> method + will only be called when it is directly combined with + <function>echo</function> or <function>print</function>. + </para> + <example> + <title>Cases where <literal>__toString</literal> is called</title> + <programlisting role="php"> +<![CDATA[ +<?php +// __toString called +echo $class; + +// __toString called (still a normal parameter for echo) +echo 'text', $class; + +// __toString not called (concatenation operator used first) +echo 'text' . $class; + +// __toString not called (casted to string first) +echo (string) $class; +?> +]]> + </programlisting> + </example> </sect2> </sect1>