Content · Web Development · PHP

PHP Callable Type and Invokable Objects

PHP Callable Type and Invokable Objects: An object with __invoke() is callable and can encapsulate dependencies and state for a callback. See three examples and important limitations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

An object with __invoke() is callable and can encapsulate dependencies and state for a callback.

The examples below begin with the core behavior, then move to an application-oriented use.

How PHP Callable Type and Invokable Objects Works


An object with __invoke() is callable and can encapsulate dependencies and state for a callback.

Example 1: Basic Usage


This example uses a small input to make the main behavior visible.

<?php

class AddTax { public function __invoke(int $price): int { return (int) round($price * 1.2); } }
$callback = new AddTax();
echo $callback(100);

Output:

120

An object with __invoke() is callable and can encapsulate dependencies and state for a callback.

Example 2: A Different Input


A different set of values shows the same operation in another situation.

<?php

function executeCallback(callable $callback, int $number): int { return $callback($number); }
echo executeCallback(new class { public function __invoke(int $number): int { return $number + 1; } }, 8);

Output:

9

Keep callables explicit about parameter and return types to avoid accidental runtime TypeError exceptions.

Practical Example: Applying the Feature


This example shows an application-style use of the feature, with its surrounding setup visible.

<?php

class NormalizeText { public function __invoke(string $value): string { return strtolower(trim($value)); } }
$callback = new NormalizeText();
echo $callback(" READY ");

Output:

ready

The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.

Important Behavior and Edge Cases


  • Keep callables explicit about parameter and return types to avoid accidental runtime TypeError exceptions.
  • An object with __invoke() is callable and can encapsulate dependencies and state for a callback.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
PHP Callable Type and Invokable ObjectsAn object with __invoke() is callable and can encapsulate dependencies and state for a callback.
Alternative approachClosures are good for short inline callbacks; invokable classes scale better with reusable logic.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of PHP Callable Type and Invokable ObjectsKeep callables explicit about parameter and return types to avoid accidental runtime TypeError exceptions.
Expecting a different type of resultRead the declared return type and distinguish returned values from changes to the source object.
Assuming a related API works identicallyClosures are good for short inline callbacks; invokable classes scale better with reusable logic.

Key Takeaways


  • An object with __invoke() is callable and can encapsulate dependencies and state for a callback.
  • Keep callables explicit about parameter and return types to avoid accidental runtime TypeError exceptions.
  • Use the example that matches the values, runtime version and dependencies in your application.

PHP Callable Type and Invokable Objects is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.

Share this resourceSend this page to someone who may find it useful.