-
Notifications
You must be signed in to change notification settings - Fork 102
/
bindto.xml
164 lines (144 loc) · 4.58 KB
/
bindto.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
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 2a4874ed531bcc223ca4bc1b4f9a5292594abaea Maintainer: mowangjuanzi Status: ready -->
<!-- CREDITS: mowangjuanzi -->
<refentry xml:id="closure.bindto" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>Closure::bindTo</refname>
<refpurpose>
用特定的绑定对象和类作用域复制闭包。
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis role="Closure">
<modifier>public</modifier> <type class="union"><type>Closure</type><type>null</type></type><methodname>Closure::bindTo</methodname>
<methodparam><type class="union"><type>object</type><type>null</type></type><parameter>newThis</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>object</type><type>string</type><type>null</type></type><parameter>newScope</parameter><initializer>"static"</initializer></methodparam>
</methodsynopsis>
<para>
创建并返回一个 <link linkend="functions.anonymous">匿名函数</link>,
它与当前对象的函数体相同、绑定了同样变量,但可以绑定不同的对象,也可以绑定新的类作用域。
</para>
<para>
“绑定的对象”决定了函数体中的 <literal>$this</literal>
的取值,“类作用域”代表一个类型、决定在这个匿名函数中能够调用哪些 private 和 protected 的方法。
也就是说,此时 $this 可以调用的方法,与 <parameter>newScope</parameter> 类的成员函数是相同的。
</para>
<para>
静态闭包不能有绑定的对象(
<parameter>newThis</parameter> 参数的值应该设为
&null;)不过仍然可以用 bindTo 方法来改变它们的类作用域。
</para>
<para>
此函数确保对于非静态闭包,拥有绑定实例也意味着被限定作用域,反之亦然。
为此,非静态闭包给定一个 &null; 实例的作用域可以使其变为静态,
非静态无作用域的闭包给定一个非 null 的实例作用在一个非指定类。
</para>
<note>
<para>
如果你只是想要复制一个匿名函数,可以用
<link linkend="language.oop5.cloning">cloning</link> 代替。
</para>
</note>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>newThis</parameter></term>
<listitem>
<para>
绑定给匿名函数的一个对象,或者
&null; 来取消绑定。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>newScope</parameter></term>
<listitem>
<para>
关联到匿名函数的类作用域,或者
'static' 保持当前状态。如果是一个对象,则使用这个对象的类型为新的类作用域。
这会决定绑定的对象的 保护、私有成员 方法的可见性。
不允许内部类(的对象)作为参数传递。
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
返回新创建的 <classname>Closure</classname> 对象,
或者失败时返回 &null; 。
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>Closure::bindTo</function> 实例</title>
<programlisting role="php">
<![CDATA[
<?php
class A
{
private $val;
public function __construct($val)
{
$this->val = $val;
}
public function getClosure()
{
// 返回绑定到此对象和作用域的闭包
return function() {
return $this->val;
};
}
}
$ob1 = new A(1);
$ob2 = new A(2);
$cl = $ob1->getClosure();
echo $cl(), "\n";
$cl = $cl->bindTo($ob2);
echo $cl(), "\n";
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
1
2
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><link linkend="functions.anonymous">匿名函数</link></member>
<member><methodname>Closure::bind</methodname></member>
</simplelist>
</refsect1>
</refentry>
<!-- 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
-->