Clarified when __toString is called (bug #30379)

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@170420 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Aidan Lister 2004-10-11 06:27:51 +00:00
parent 123d3900f6
commit 7c461dd6d5

View file

@ -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 @@ $class = new TestClass('Hello');
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>