Source of: /manual/en/function.is-callable.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.var.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'function.is-callable.php',
1 => 'is_callable',
),
'up' =>
array (
0 => 'ref.var.php',
1 => 'Variable handling Functions',
),
'prev' =>
array (
0 => 'function.is-buffer.php',
1 => 'is_buffer',
),
'next' =>
array (
0 => 'function.is-double.php',
1 => 'is_double',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="function.is-callable" class="refentry">
<div class="refnamediv">
<h1 class="refname">is_callable</h1>
<p class="verinfo">(PHP 4 >= 4.0.6, PHP 5)</p><p class="refpurpose"><span class="refname">is_callable</span> — <span class="dc-title">
Verify that the contents of a variable can be called as a function
</span></p>
</div>
<a name="function.is-callable.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="type">bool</span> <span class="methodname"><b>is_callable</b></span>
( <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.callback" class="type callback">callback</a></span> <tt class="parameter">$name</tt></span>
[, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$syntax_only</tt><span class="initializer"> = false</span></span>
[, <span class="methodparam"><span class="type">string</span> <tt class="parameter reference">&$callable_name</tt></span>
]] )</div>
<p class="para rdfs-comment">
Verify that the contents of a variable can be called as a function.
This can check that a simple variable contains the name of a valid
function, or that an array contains a properly encoded object and
function name.
</p>
</div>
<a name="function.is-callable.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">name</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Can be either the name of a function stored in a string variable, or
an object and the name of a method within the object, like this:
<div class="example-contents screen">array($SomeObject, 'MethodName')</div>
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">syntax_only</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
If set to <b><tt class="constant">TRUE</tt></b> the function only verifies that
<i><tt class="parameter">name</tt></i>
might be a function or method. It will only
reject simple variables that are not strings, or an array that does
not have a valid structure to be used as a callback. The valid ones
are supposed to have only 2 entries, the first of which is an object
or a string, and the second a string.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">callable_name</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Receives the "callable name". In the example below it is
"someClass::someMethod". Note, however, that despite the implication
that someClass::SomeMethod() is a callable static method, this is not
the case.
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="function.is-callable.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
Returns <b><tt class="constant">TRUE</tt></b> if <i><tt class="parameter">name</tt></i>
is callable, <b><tt class="constant">FALSE</tt></b>
otherwise.
</p>
</div>
<a name="function.is-callable.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 <b>is_callable()</b> example</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// How to check a variable to see if it can be called<br />// as a function.<br /><br />//<br />// Simple variable containing a function<br />//<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">someFunction</span><span style="color: #007700">() <br />{<br />}<br /><br /></span><span style="color: #0000BB">$functionVariable </span><span style="color: #007700">= </span><span style="color: #DD0000">'someFunction'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">is_callable</span><span style="color: #007700">(</span><span style="color: #0000BB">$functionVariable</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">$callable_name</span><span style="color: #007700">)); </span><span style="color: #FF8000">// bool(true)<br /><br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$callable_name</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// someFunction<br /><br />//<br />// Array containing a method<br />//<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">someClass </span><span style="color: #007700">{<br /><br /> function </span><span style="color: #0000BB">someMethod</span><span style="color: #007700">() <br /> {<br /> }<br /><br />}<br /><br /></span><span style="color: #0000BB">$anObject </span><span style="color: #007700">= new </span><span style="color: #0000BB">someClass</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$methodVariable </span><span style="color: #007700">= array(</span><span style="color: #0000BB">$anObject</span><span style="color: #007700">, </span><span style="color: #DD0000">'someMethod'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">is_callable</span><span style="color: #007700">(</span><span style="color: #0000BB">$methodVariable</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">, </span><span style="color: #0000BB">$callable_name</span><span style="color: #007700">)); </span><span style="color: #FF8000">// bool(true)<br /><br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$callable_name</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// someClass::someMethod<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</div>
<a name="function.is-callable.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="function.function-exists.php" class="function" rel="rdfs-seeAlso">function_exists()</a> - Return TRUE if the given function has been defined</li>
<li class="member"><a href="function.method-exists.php" class="function" rel="rdfs-seeAlso">method_exists()</a> - Checks if the class method exists</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>