mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
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:
parent
123d3900f6
commit
7c461dd6d5
1 changed files with 35 additions and 7 deletions
|
@ -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>
|
||||
|
||||
|
|
Loading…
Reference in a new issue