The feature illustrated below operates on PHP values, objects, or language metadata.
What Is is_callable in PHP?
Checks whether a value can be called as a function, including closures, invokable objects and eligible method references.
is_callable(mixed $value, bool $syntax_only = ..., $callable_name = ...) : bool
Parameters:
$value— Required. The value to store, compare or inspect.$syntax_only— Optional. The syntax_only argument used to configure this operation; its accepted values follow the declared PHP type.$callable_name— Optional. The callable_name argument used to configure this operation; its accepted values follow the declared PHP type.
Return type: bool. is_callable() can report a callable that still fails if called with the wrong arguments.
Example 1: Understanding the Basic Behavior
<?php
$formatter = static fn(string $value): string => strtoupper($value);
var_dump(is_callable($formatter));
Output:
bool(true)
Explanation: Checks whether a value can be called as a function, including closures, invokable objects and eligible method references.
Example 2: Testing Another Case
<?php
class Handler { public function handle(): void {} }
var_dump(is_callable([new Handler(), 'handle']));
Output:
bool(true)
Explanation: is_callable() can report a callable that still fails if called with the wrong arguments.
Practical Example: Applying the Operation
<?php
$candidate = 'not_real_function_101';
if (!is_callable($candidate)) { echo 'Not callable'; }
Output:
Not callable
Explanation: A method name can be callable only in a valid visibility and object context.
Important Behavior and Edge Cases
- is_callable() can report a callable that still fails if called with the wrong arguments.
- A method name can be callable only in a valid visibility and object context.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
is_callable | Checks whether a value can be called as a function, including closures, invokable objects and eligible method references. |
method_exists() | method_exists() tests whether a method is declared; is_callable() tests whether the supplied callable form is invocable in context. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Calling a callable with unsupported argument types simply because is_callable() returned true. | is_callable() can report a callable that still fails if called with the wrong arguments. |
| Choosing method_exists() without checking whether it provides the same result. | method_exists() tests whether a method is declared; is_callable() tests whether the supplied callable form is invocable in context. |
Key Takeaways
- Checks whether a value can be called as a function, including closures, invokable objects and eligible method references.
- is_callable() can report a callable that still fails if called with the wrong arguments.
- A method name can be callable only in a valid visibility and object context.
The examples demonstrate where is_callable fits within php programming. Verify the relevant preconditions whenever you adapt the code to real data.