> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP callable

A `callable` in PHP is a pseudo-type used to denote a variable that can be invoked as a function. It acts as a type declaration for arguments or return values, ensuring the provided value resolves to an executable routine within the PHP engine.

Internally, PHP evaluates callables to either a `Closure` object or a specific string/array structure that the engine's invocation handlers can resolve.

## Callable Structures and Syntax

PHP recognizes several distinct data structures as valid callables:

**1. Standard Functions**
Passed as a string containing the exact, fully qualified name of the function.

```php theme={"dark"}
$callable = 'strlen';
$callable = '\Namespace\function_name';
```

**2. Anonymous Functions (Closures)**
Functions without a specified name, which PHP internally compiles into instances of the `Closure` class.

```php theme={"dark"}
$callable = function(int $x): int {
    return $x * 2;
};
```

**3. Arrow Functions**
A concise syntax for closures that automatically captures variables from the parent scope by value.

```php theme={"dark"}
$callable = fn(int $x): int => $x * 2;
```

**4. Object Methods**
Passed as a sequentially indexed array containing exactly two elements: the object instance at index `0`, and the method name as a string at index `1`.

```php theme={"dark"}
$obj = new MyClass();
$callable = [$obj, 'methodName'];
```

**5. Static Class Methods**
Passed either as a two-element array containing the class name (string) and method name (string), or as a single concatenated string using the scope resolution operator (`::`).

```php theme={"dark"}
$callable = ['MyClass', 'staticMethodName'];
$callable = 'MyClass::staticMethodName';
```

**6. Invokable Objects**
An instance of a class that implements the `__invoke()` magic method. The object itself is passed as the callable.

```php theme={"dark"}
class InvokableClass {
    public function __invoke(int $x) {
        return $x;
    }
}
$callable = new InvokableClass();
```

**7. First-Class Callable Syntax (PHP 8.1+)**
A syntax that creates an anonymous function (`Closure`) from any existing callable using the `(...)` operator. This provides static analysis benefits and scope resolution at the time of creation rather than execution.

```php theme={"dark"}
$callable = strlen(...);
$callable = $obj->methodName(...);
$callable = MyClass::staticMethodName(...);
```

## Invocation and Verification

A variable typed as `callable` can be executed using variable function syntax or built-in execution functions.

```php theme={"dark"}
// Variable invocation syntax
$result = $callable($arg1, $arg2);

// Built-in execution functions
$result = call_user_func($callable, $arg1, $arg2);
$result = call_user_func_array($callable, [$arg1, $arg2]);
```

To verify if a variable holds a valid callable structure before execution, PHP provides the `is_callable()` function. This function evaluates the structure and returns a boolean indicating whether the contents can be invoked within the current execution scope.

```php theme={"dark"}
if (is_callable($variable)) {
    $variable();
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
