PHP __invoke

Created with Sketch.

PHP __invoke

Summary: in this tutorial, you’ll learn about the PHP __invoke() magic method and how to use it effectively.

Introduction to the PHP __invoke() magic method

Suppose that you have a class called MyClass:

class MyClass
{
// ...
}

Code language: PHP (php)

Typically, you create a new instance of the MyClass and access its methods and properties like this:

$instance = new MyClass();
$instance->methodName();

Code language: PHP (php)

Or if the MyClass has static methods, you can access them using the :: operator:

MyClass::staticMethod();

Code language: PHP (php)

Besides using the MyClass these ways, PHP allows you to use the object of the class as a function. For example:

$instance($arguments);

Code language: PHP (php)

In this case, PHP will call the __invoke() method of the MyClass. For example:

<?php

class MyClass
{
public function __invoke(...$arguments)
{
echo 'Called to the __invoke method';
}
}

$instance = new MyClass;
$instance();

Code language: PHP (php)

Output:

Called to the __invoke method

Code language: PHP (php)

The $instance is known as a function object or functor.

The __invoke() magic method has the following syntax:

__invoke( ...$values): mixed

Code language: PHP (php)

PHP will call the __invoke() magic method when you call an object as a function.

Also, the object of the class that implements the __invoke() magic method is a callable. For example:

echo is_callable($instance) ? 'yes' : 'no'; // yes

Code language: PHP (php)

In this example, the $instance of the MyClass is a callable. This means that you can pass it to any function or method that accepts a callable.

Practical PHP __invoke() magic method example

Suppose that you have an array of customer data like this;

$customers = [
['id' => 1, 'name' => 'John', 'credit' => 20000],
['id' => 3, 'name' => 'Alice', 'credit' => 10000],
['id' => 2, 'name' => 'Bob', 'credit' => 15000]
];

Code language: PHP (php)

To sort the customers by name or credit, you can use the usort() function. The second parameter of the usort() function is a callable that determines the sort order:

usort(array &$array, callable $callback): bool

Code language: PHP (php)

The following defines the class Comparator that implements the __invoke() magic method:

<?php

class Comparator
{
private $key;

public function __construct(string $key)
{
$this->key = $key;
}

public function __invoke($a, $b)
{
return $a[$this->key] <=> $b[$this->key];
}
}

Code language: PHP (php)

The __invoke() method returns the result of the comparison of two array elements by a specified key.

To use the Comparator class, you can create a new instance of the class and pass it to the usort() function as follows:

usort($customers, new Comparator('name'));

Code language: PHP (php)

This statement sorts the customers by name.

To sort the customers by credit, you can use the credit as the key like this:

usort($customers, new Comparator('credit'));

Code language: PHP (php)

Put it all together.

<?php

class Comparator
{
private $key;

public function __construct(string $key)
{
$this->key = $key;
}

public function __invoke($a, $b)
{
return $a[$this->key] <=> $b[$this->key];
}
}

$customers = [
['id' => 1, 'name' => 'John', 'credit' => 20000],
['id' => 3, 'name' => 'Alice', 'credit' => 10000],
['id' => 2, 'name' => 'Bob', 'credit' => 15000]
];

// sort customers by names
usort($customers, new Comparator('name'));
print_r($customers);

// sort customers by credit
usort($customers, new Comparator('credit'));
print_r($customers);

Code language: PHP (php)

Summary

  • Use the __invoke() magic method is invoked when you call an object as a function.
  • An object of a class that implements the __invoke() is known as a function object or functor. It’s also a callable.

Leave a Reply

Your email address will not be published. Required fields are marked *