File: magic.xml

package info (click to toggle)
php-doc 20061001-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 45,764 kB
  • ctags: 1,611
  • sloc: xml: 502,485; php: 7,645; cpp: 500; makefile: 297; perl: 161; sh: 141; awk: 28
file content (180 lines) | stat: -rw-r--r-- 5,128 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.13 $ -->
 <sect1 id="language.oop5.magic">
  <title>Magic Methods</title>
  <para>
   The function names
   <literal>__construct</literal>,
   <literal>__destruct</literal>
   (see <link linkend="language.oop5.decon">Constructors and Destructors</link>),
   <literal>__call</literal>,
   <literal>__get</literal>,
   <literal>__set</literal>,
   <literal>__isset</literal>,
   <literal>__unset</literal>
   (see <link linkend="language.oop5.overloading">Overloading</link>),
   <literal>__sleep</literal>,
   <literal>__wakeup</literal>,
   <literal>__toString</literal>,
   <literal>__set_state</literal>,
   <link linkend="language.oop5.cloning">__clone</link> and
   <link linkend="language.oop5.autoload">__autoload</link>
   are magical in PHP classes. You
   cannot have functions with these names in any of your
   classes unless you want the magic functionality associated
   with them.
  </para>

  <caution>
   <simpara>
    PHP reserves all function names starting with __ as magical.
    It is recommended that you do not use function names with
    __ in PHP unless you want some documented magic functionality.
   </simpara>
  </caution>
 
  <sect2 id="language.oop5.magic.sleep">
   <title><literal>__sleep</literal> and <literal>__wakeup</literal></title>
   <para>
    <function>serialize</function> checks if your class has a function with
    the magic name <literal>__sleep</literal>. If so, that function is
    executed prior to any serialization. It can clean up the object
    and is supposed to return an array with the names of all variables
    of that object that should be serialized.
   </para>
   <para>
    The intended use of <literal>__sleep</literal> is to close any
    database connections that the object may have, commit pending
    data or perform similar cleanup tasks. Also, the function is
    useful if you have very large objects which do not need to be
    saved completely.
   </para>
   <para>
    Conversely, <function>unserialize</function> checks for the
    presence of a function with the magic name 
    <literal>__wakeup</literal>. If present, this function can
    reconstruct any resources that the object may have.
   </para>
   <para>
    The intended use of <literal>__wakeup</literal> is to
    reestablish any database connections that may have been lost
    during serialization and perform other reinitialization
    tasks.
   </para>
   <example>
    <title>Sleep and wakeup</title>
    <programlisting role="php">
<![CDATA[
<?php
class Connection {
    protected $link;
    private $server, $username, $password, $db;
    
    public function __construct($server, $username, $password, $db)
    {
        $this->server = $server;
        $this->username = $username;
        $this->password = $password;
        $this->db = $db;
        $this->connect();
    }
    
    private function connect()
    {
        $this->link = mysql_connect($this->server, $this->username, $this->password);
        mysql_select_db($this->db, $this->link);
    }
    
    public function __sleep()
    {
        mysql_close($this->link);
    }
    
    public function __wakeup()
    {
        $this->connect();
    }
}
?>
]]>
    </programlisting>
   </example>
  </sect2>

  <sect2 id="language.oop5.magic.tostring">
   <title><literal>__toString</literal></title>
   <para>
    The <literal>__toString</literal> method allows a class to decide
    how it will react when it is converted to a string.
   </para>
   <example>
    <title>Simple example</title>
    <programlisting role="php">
<![CDATA[
<?php
// Declare a simple class
class TestClass
{
    public $foo;

    public function __construct($foo) {
        $this->foo = $foo;
    }

    public function __toString() {
        return $this->foo;
    }
}

$class = new TestClass('Hello');
echo $class;
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
Hello
]]>
    </screen>
   </example>
   <para>
    It is worth noting that before PHP 5.2.0 the <literal>__toString</literal>
    method was only called when it was directly combined with
    <function>echo</function> or <function>print</function>.
   </para>
  </sect2>

  <sect2 id="language.oop5.magic.set-state">
   <title><literal>__set_state</literal></title>
   <para>
    This <link linkend="language.oop5.static">static</link> method is called
    for classes exported by <function>var_export</function> since PHP 5.1.0.
   </para>
   <para>
    The only parameter of this method is an array containing exported
    properties in the form <literal>array('property' => value, ...)</literal>.
   </para>
  </sect2>
 </sect1>
 
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->