-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathoverloading.xml
303 lines (253 loc) · 9.55 KB
/
overloading.xml
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 2ab7e9d763666526942ed4477c4f876beb160892 Maintainer: Haohappy Status: ready -->
<!-- CREDITS: mowangjuanzi -->
<sect1 xml:id="language.oop5.overloading" xmlns="http://docbook.org/ns/docbook">
<title>重载</title>
<para>
PHP所提供的<quote>重载</quote>(overloading)是指动态地<quote>创建</quote>类属性和方法。我们是通过魔术方法(magic methods)来实现的。
</para>
<para>
当调用当前环境下未定义或不<link
linkend="language.oop5.visibility">可见</link>的类属性或方法时,重载方法会被调用。本节后面将使用<quote>不可访问属性(inaccessible
properties)</quote>和<quote>不可访问方法(inaccessible
methods)</quote>来称呼这些未定义或不可见的类属性或方法。
</para>
<para>
所有的重载方法都必须被声明为 <literal>public</literal>。
</para>
<note>
<para>
这些魔术方法的参数都不能<link linkend="functions.arguments.by-reference">通过引用传递</link>。
</para>
</note>
<note>
<para>
PHP中的<quote>重载</quote>与其它绝大多数面向对象语言不同。传统的<quote>重载</quote>是用于提供多个同名的类方法,但各方法的参数类型和个数不同。
</para>
</note>
<sect2 xml:id="language.oop5.overloading.members">
<title>属性重载</title>
<methodsynopsis xml:id="object.set">
<modifier>public</modifier> <type>void</type><methodname>__set</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
</methodsynopsis>
<methodsynopsis xml:id="object.get">
<modifier>public</modifier> <type>mixed</type><methodname>__get</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
</methodsynopsis>
<methodsynopsis xml:id="object.isset">
<modifier>public</modifier> <type>bool</type><methodname>__isset</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
</methodsynopsis>
<methodsynopsis xml:id="object.unset">
<modifier>public</modifier> <type>void</type><methodname>__unset</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
</methodsynopsis>
<para>
在给不可访问(protected 或 private)或不存在的属性赋值时,<link linkend="object.set">__set()</link> 会被调用。
</para>
<para>
读取不可访问(protected 或 private)或不存在的属性的值时,<link linkend="object.get">__get()</link> 会被调用。
</para>
<para>
当对不可访问(protected 或 private)或不存在的属性调用 <function>isset</function> 或
<function>empty</function> 时,<link linkend="object.isset">__isset()</link>
会被调用。
</para>
<para>
当对不可访问(protected 或 private)或不存在的属性调用 <function>unset</function>
时,<link linkend="object.unset">__unset()</link> 会被调用。
</para>
<para>
参数 <varname>$name</varname> 是指要操作的变量名称。<link linkend="object.set">__set()</link>
方法的 <varname>$value</varname> 参数指定了 <varname>$name</varname> 变量的值。
</para>
<para>
属性重载只能在对象中进行。在静态方法中,这些魔术方法将不会被调用。所以这些方法都不能被
声明为 <link linkend="language.oop5.static">static</link>。将这些魔术方法定义为 <literal>static</literal> 会产生一个警告。
</para>
<note>
<para>
因为 PHP 处理赋值运算的方式,<link linkend="object.set">__set()</link>
的返回值将被忽略。类似的, 在下面这样的链式赋值中,<link linkend="object.get">__get()</link>
不会被调用:<literal><![CDATA[ $a = $obj->b = 8; ]]></literal>
</para>
</note>
<note>
<para>
PHP 不会在重载方法中再次调用本身。这意味着如果没有定义 <literal>foo</literal> 属性,在
<link linkend="object.get">__get()</link> 中执行 <code>return $this->foo</code>
将会返回 <literal>null</literal> 并引发 <constant>E_WARNING</constant>,而不是再次调用
<link linkend="object.get">__get()</link>。然而,重载方法可能会隐式调用其它重载方法(比如调用
<link linkend="object.set">__set()</link> 触发 <link linkend="object.get">__get()</link>)。
</para>
</note>
<example>
<title>使用 <link linkend="object.get">__get()</link>,<link
linkend="object.set">__set()</link>,<link linkend="object.isset">__isset()</link>
和 <link linkend="object.unset">__unset()</link> 进行属性重载</title>
<programlisting role="php">
<![CDATA[
<?php
class PropertyTest {
/** 被重载的数据保存在此 */
private $data = array();
/** 重载不能被用在已经定义的属性 */
public $declared = 1;
/** 只有从类外部访问这个属性时,重载才会发生 */
private $hidden = 2;
public function __set($name, $value)
{
echo "Setting '$name' to '$value'\n";
$this->data[$name] = $value;
}
public function __get($name)
{
echo "Getting '$name'\n";
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
public function __isset($name)
{
echo "Is '$name' set?\n";
return isset($this->data[$name]);
}
public function __unset($name)
{
echo "Unsetting '$name'\n";
unset($this->data[$name]);
}
/** 非魔术方法 */
public function getHidden()
{
return $this->hidden;
}
}
echo "<pre>\n";
$obj = new PropertyTest;
$obj->a = 1;
echo $obj->a . "\n\n";
var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo "\n";
echo $obj->declared . "\n\n";
echo "Let's experiment with the private property named 'hidden':\n";
echo "Privates are visible inside the class, so __get() not used...\n";
echo $obj->getHidden() . "\n";
echo "Privates not visible outside of class, so __get() is used...\n";
echo $obj->hidden . "\n";
?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Setting 'a' to '1'
Getting 'a'
1
Is 'a' set?
bool(true)
Unsetting 'a'
Is 'a' set?
bool(false)
1
Let's experiment with the private property named 'hidden':
Privates are visible inside the class, so __get() not used...
2
Privates not visible outside of class, so __get() is used...
Getting 'hidden'
Notice: Undefined property via __get(): hidden in <file> on line 70 in <file> on line 29
]]>
</screen>
</example>
</sect2>
<sect2 xml:id="language.oop5.overloading.methods">
<title>方法重载</title>
<methodsynopsis xml:id="object.call">
<modifier>public</modifier> <type>mixed</type><methodname>__call</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam><type>array</type><parameter>arguments</parameter></methodparam>
</methodsynopsis>
<methodsynopsis xml:id="object.callstatic">
<modifier>public static</modifier> <type>mixed</type><methodname>__callStatic</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam><type>array</type><parameter>arguments</parameter></methodparam>
</methodsynopsis>
<para>
在对象中调用一个不可访问方法时,<link linkend="object.call">__call()</link> 会被调用。
</para>
<para>
在静态上下文中调用一个不可访问方法时,<link linkend="object.callstatic">__callStatic()</link> 会被调用。
</para>
<para>
<varname>$name</varname> 参数是要调用的方法名称。<varname>$arguments</varname>
参数是一个枚举数组,包含着要传递给方法 <varname>$name</varname> 的参数。
</para>
<example>
<title>使用 <link linkend="object.call">__call()</link>
和 <link linkend="object.callstatic">__callStatic()</link> 对方法重载</title>
<programlisting role="php">
<![CDATA[
<?php
class MethodTest
{
public function __call($name, $arguments)
{
// 注意: $name 的值区分大小写
echo "Calling object method '$name' "
. implode(', ', $arguments). "\n";
}
public static function __callStatic($name, $arguments)
{
// 注意: $name 的值区分大小写
echo "Calling static method '$name' "
. implode(', ', $arguments). "\n";
}
}
$obj = new MethodTest;
$obj->runTest('in object context');
MethodTest::runTest('in static context');
?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Calling object method 'runTest' in object context
Calling static method 'runTest' in static context
]]>
</screen>
</example>
</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:"~/.phpdoc/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
-->