|
17 | 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 a value is a <type>callable</type>. |
| 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. |
21 | 23 | </para>
|
22 | 24 | </refsect1>
|
23 | 25 |
|
|
29 | 31 | <term><parameter>value</parameter></term>
|
30 | 32 | <listitem>
|
31 | 33 | <para>
|
32 |
| - The value to check |
| 34 | + The value to be checked. |
33 | 35 | </para>
|
34 | 36 | </listitem>
|
35 | 37 | </varlistentry>
|
|
38 | 40 | <listitem>
|
39 | 41 | <para>
|
40 | 42 | If set to &true; the function only verifies that
|
41 |
| - <parameter>value</parameter> might be a function or method. It will only |
42 |
| - reject simple variables that are not strings, or an array that does |
43 |
| - not have a valid structure to be used as a callback. The valid ones |
44 |
| - 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 neither callable objects, strings nor arrays |
| 45 | + with a valid structure to be used as a callback. A valid callable array |
| 46 | + has 2 entries, the first of which is an object |
45 | 47 | or a string, and the second a string.
|
46 | 48 | </para>
|
47 | 49 | </listitem>
|
|
50 | 52 | <term><parameter>callable_name</parameter></term>
|
51 | 53 | <listitem>
|
52 | 54 | <para>
|
53 |
| - Receives the "callable name". In the example below it is |
54 |
| - "someClass::someMethod". Note, however, that despite the implication |
55 |
| - that someClass::SomeMethod() is a callable static method, this is not |
56 |
| - the case. |
| 55 | + Receives the "callable name", e.g. |
| 56 | + <literal>"SomeClass::someMethod"</literal>. Note, however, that despite |
| 57 | + the implication that <literal>SomeClass::someMethod()</literal> is a |
| 58 | + callable static method, this is not the case. |
57 | 59 | </para>
|
58 | 60 | </listitem>
|
59 | 61 | </varlistentry>
|
|
73 | 75 | &reftitle.examples;
|
74 | 76 | <para>
|
75 | 77 | <example>
|
76 |
| - <title><function>is_callable</function> example</title> |
| 78 | + <title>Checking whether a string can be called as a function</title> |
77 | 79 | <programlisting role="php">
|
78 | 80 | <![CDATA[
|
79 | 81 | <?php
|
80 |
| -// How to check a variable to see if it can be called |
81 |
| -// as a function. |
82 |
| -
|
83 |
| -// |
84 |
| -// Simple variable containing a function |
85 |
| -// |
86 | 82 |
|
87 | 83 | function someFunction() {}
|
88 | 84 |
|
89 | 85 | $functionVariable = 'someFunction';
|
90 | 86 |
|
91 |
| -var_dump(is_callable($functionVariable, false, $callable_name)); // bool(true) |
| 87 | +var_dump(is_callable($functionVariable, false, $callable_name)); |
92 | 88 |
|
93 |
| -echo $callable_name, "\n"; // someFunction |
| 89 | +var_dump($callable_name); |
94 | 90 |
|
95 |
| -// |
96 |
| -// Array containing a method |
97 |
| -// |
| 91 | +?> |
| 92 | +]]> |
| 93 | + </programlisting> |
| 94 | + &example.outputs; |
| 95 | + <screen> |
| 96 | +<![CDATA[ |
| 97 | +bool(true) |
| 98 | +string(12) "someFunction" |
| 99 | +]]> |
| 100 | + </screen> |
| 101 | + </example> |
| 102 | + <example> |
| 103 | + <title>Checking whether an array can be called as a function</title> |
| 104 | + <programlisting role="php"> |
| 105 | +<![CDATA[ |
| 106 | +<?php |
98 | 107 |
|
99 |
| -class someClass |
| 108 | +class SomeClass |
100 | 109 | {
|
101 | 110 | public function someMethod() {}
|
102 | 111 | }
|
103 | 112 |
|
104 |
| -$anObject = new someClass(); |
| 113 | +$anObject = new SomeClass(); |
105 | 114 |
|
106 | 115 | $methodVariable = [$anObject, 'someMethod'];
|
107 | 116 |
|
108 |
| -var_dump(is_callable($methodVariable, true, $callable_name)); // bool(true) |
| 117 | +var_dump(is_callable($methodVariable, true, $callable_name)); |
109 | 118 |
|
110 |
| -echo $callable_name, "\n"; // someClass::someMethod |
| 119 | +var_dump($callable_name); |
111 | 120 |
|
112 | 121 | ?>
|
113 | 122 | ]]>
|
114 | 123 | </programlisting>
|
115 |
| - </example> |
| 124 | + &example.outputs; |
| 125 | + <screen> |
| 126 | +<![CDATA[ |
| 127 | +bool(true) |
| 128 | +string(21) "SomeClass::someMethod" |
| 129 | +]]> |
| 130 | + </screen> |
| 131 | + </example> |
116 | 132 | <example>
|
117 | 133 | <title><function>is_callable</function> and constructors</title>
|
118 | 134 | <simpara>
|
@@ -173,6 +189,7 @@ bool(false)
|
173 | 189 | &reftitle.seealso;
|
174 | 190 | <para>
|
175 | 191 | <simplelist>
|
| 192 | + <member><function>call_user_func</function></member> |
176 | 193 | <member><function>function_exists</function></member>
|
177 | 194 | <member><function>method_exists</function></member>
|
178 | 195 | </simplelist>
|
|
0 commit comments