reference/var/functions/is-callable.xml
32c55286c8739fd20de9c8624473b2f3268de68e
32c55286c8739fd20de9c8624473b2f3268de68e
...
...
@@ -4,7 +4,7 @@
4
4
<refnamediv>
5
5
<refname>is_callable</refname>
6
6
<refpurpose>
7
-
Verify that the contents of a variable can be called as a function
7
+
Verify that a value can be called as a function from the current scope
8
8
</refpurpose>
9
9
</refnamediv>
10
10
...
...
@@ -12,15 +12,14 @@
12
12
&reftitle.description;
13
13
<methodsynopsis>
14
14
<type>bool</type><methodname>is_callable</methodname>
15
-
<methodparam><type>mixed</type><parameter>var</parameter></methodparam>
16
-
<methodparam choice="opt"><type>bool</type><parameter>syntax_only</parameter><initializer>false</initializer></methodparam>
17
-
<methodparam choice="opt"><type>string</type><parameter role="reference">callable_name</parameter></methodparam>
15
+
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
16
+
<methodparam choice="opt"><type>bool</type><parameter>syntax_only</parameter><initializer>&false;</initializer></methodparam>
17
+
<methodparam choice="opt"><type>string</type><parameter role="reference">callable_name</parameter><initializer>&null;</initializer></methodparam>
18
18
</methodsynopsis>
19
19
<para>
20
-
Verify that the contents of a variable can be called as a function.
21
-
This can check that a simple variable contains the name of a valid
22
-
function, or that an array contains a properly encoded object and
23
-
function name.
20
+
Verifies that <parameter>value</parameter> is a <type>callable</type>,
21
+
or that it can be called using the
22
+
<function>call_user_func</function> function.
24
23
</para>
25
24
</refsect1>
26
25
...
...
@@ -29,10 +28,10 @@
29
28
<para>
30
29
<variablelist>
31
30
<varlistentry>
32
-
<term><parameter>var</parameter></term>
31
+
<term><parameter>value</parameter></term>
33
32
<listitem>
34
33
<para>
35
-
The value to check
34
+
The value to be checked.
36
35
</para>
37
36
</listitem>
38
37
</varlistentry>
...
...
@@ -41,10 +40,11 @@
41
40
<listitem>
42
41
<para>
43
42
If set to &true; the function only verifies that
44
-
<parameter>name</parameter> might be a function or method. It will only
45
-
reject simple variables that are not strings, or an array that does
46
-
not have a valid structure to be used as a callback. The valid ones
47
-
are supposed to have only 2 entries, the first of which is an object
43
+
<parameter>value</parameter> might be a function or method. It will
44
+
reject any values that are not <link linkend="object.invoke">invokable</link> objects,
45
+
<classname>Closure</classname>, &string;s, or &array;s that do not have
46
+
a valid structure to be used as a callback. A valid callable array
47
+
has 2 entries, the first of which is an object
48
48
or a string, and the second a string.
49
49
</para>
50
50
</listitem>
...
...
@@ -53,10 +53,10 @@
53
53
<term><parameter>callable_name</parameter></term>
54
54
<listitem>
55
55
<para>
56
-
Receives the "callable name". In the example below it is
57
-
"someClass::someMethod". Note, however, that despite the implication
58
-
that someClass::SomeMethod() is a callable static method, this is not
59
-
the case.
56
+
Receives the "callable name", e.g.
57
+
<literal>"SomeClass::someMethod"</literal>. Note, however, that despite
58
+
the implication that <literal>SomeClass::someMethod()</literal> is a
59
+
callable static method, this is not the case.
60
60
</para>
61
61
</listitem>
62
62
</varlistentry>
...
...
@@ -67,7 +67,7 @@
67
67
<refsect1 role="returnvalues">
68
68
&reftitle.returnvalues;
69
69
<para>
70
-
Returns &true; if <parameter>var</parameter> is callable, &false;
70
+
Returns &true; if <parameter>value</parameter> is callable, &false;
71
71
otherwise.
72
72
</para>
73
73
</refsect1>
...
...
@@ -76,58 +76,128 @@
76
76
&reftitle.examples;
77
77
<para>
78
78
<example>
79
-
<title><function>is_callable</function> example</title>
79
+
<title>Checking whether a string can be called as a function</title>
80
80
<programlisting role="php">
81
81
<![CDATA[
82
82
<?php
83
-
// How to check a variable to see if it can be called
84
-
// as a function.
85
83
86
-
//
87
-
// Simple variable containing a function
88
-
//
84
+
function someFunction() {}
89
85
90
-
function someFunction()
86
+
$functionVariable = 'someFunction';
87
+
88
+
var_dump(is_callable($functionVariable, false, $callable_name));
89
+
90
+
var_dump($callable_name);
91
+
92
+
?>
93
+
]]>
94
+
</programlisting>
95
+
&example.outputs;
96
+
<screen>
97
+
<![CDATA[
98
+
bool(true)
99
+
string(12) "someFunction"
100
+
]]>
101
+
</screen>
102
+
</example>
103
+
<example>
104
+
<title>Checking whether an array can be called as a function</title>
105
+
<programlisting role="php">
106
+
<![CDATA[
107
+
<?php
108
+
109
+
class SomeClass
91
110
{
111
+
public function someMethod() {}
92
112
}
93
113
94
-
$functionVariable = 'someFunction';
114
+
$anObject = new SomeClass();
95
115
96
-
var_dump(is_callable($functionVariable, false, $callable_name)); // bool(true)
116
+
$methodVariable = [$anObject, 'someMethod'];
97
117
98
-
echo $callable_name, "\n"; // someFunction
118
+
var_dump(is_callable($methodVariable, true, $callable_name));
99
119
100
-
//
101
-
// Array containing a method
102
-
//
120
+
var_dump($callable_name);
103
121
104
-
class someClass {
122
+
?>
123
+
]]>
124
+
</programlisting>
125
+
&example.outputs;
126
+
<screen>
127
+
<![CDATA[
128
+
bool(true)
129
+
string(21) "SomeClass::someMethod"
130
+
]]>
131
+
</screen>
132
+
</example>
133
+
<example>
134
+
<title><function>is_callable</function> and constructors</title>
135
+
<simpara>
136
+
Despite the fact that constructors are the methods that are called when
137
+
an object is created, they are not static methods and
138
+
<function>is_callable</function> will return &false; for them. It's not
139
+
possible to use <function>is_callable</function> to check if a class can
140
+
be instantiated from the current scope.
141
+
</simpara>
142
+
<programlisting role="php">
143
+
<![CDATA[
144
+
<?php
105
145
106
-
function someMethod()
107
-
{
108
-
}
146
+
class Foo
147
+
{
148
+
public function __construct() {}
109
149
150
+
public function foo() {}
110
151
}
111
152
112
-
$anObject = new someClass();
113
-
114
-
$methodVariable = array($anObject, 'someMethod');
153
+
var_dump(
154
+
is_callable(['Foo', '__construct']),
155
+
is_callable(['Foo', 'foo'])
156
+
);
115
157
116
-
var_dump(is_callable($methodVariable, true, $callable_name)); // bool(true)
117
-
118
-
echo $callable_name, "\n"; // someClass::someMethod
158
+
$foo = new Foo();
159
+
var_dump(is_callable([$foo, '__construct']));
119
160
120
161
?>
121
162
]]>
122
163
</programlisting>
164
+
&example.outputs;
165
+
<screen>
166
+
<![CDATA[
167
+
bool(false)
168
+
bool(false)
169
+
bool(true)
170
+
]]>
171
+
</screen>
123
172
</example>
124
173
</para>
125
174
</refsect1>
126
175
176
+
<refsect1 role="notes">
177
+
&reftitle.notes;
178
+
<simplelist>
179
+
<member>
180
+
An object is always callable if it implements <link linkend="object.invoke">__invoke()</link>,
181
+
and that method is visible in the current scope.
182
+
</member>
183
+
<member>
184
+
A class name is callable if it implements <link linkend="object.callstatic">__callStatic()</link>.
185
+
</member>
186
+
<member>
187
+
If an object implements <link linkend="object.call">__call()</link>, then this function will
188
+
return &true; for any method on that object, even if the method is not defined.
189
+
</member>
190
+
<member>
191
+
This function may trigger autoloading if called with the name of a class.
192
+
</member>
193
+
</simplelist>
194
+
</refsect1>
195
+
127
196
<refsect1 role="seealso">
128
197
&reftitle.seealso;
129
198
<para>
130
199
<simplelist>
200
+
<member><function>call_user_func</function></member>
131
201
<member><function>function_exists</function></member>
132
202
<member><function>method_exists</function></member>
133
203
</simplelist>
...
...
@@ -135,7 +205,6 @@ echo $callable_name, "\n"; // someClass::someMethod
135
205
</refsect1>
136
206
137
207
</refentry>
138
-
139
208
<!-- Keep this comment at the end of the file
140
209
Local variables:
141
210
mode: sgml
142
211