---
title: "The art of PHP callback"
url: https://www.exakat.io/the-art-of-php-callback/
date: 2015-08-06
modified: 2020-10-06
author: "dams"
description: "The art of PHP callback PHP callback are functions that may be called dynamically by PHP. They are used by native functions such as array_map, usort, preg_replace_callback, etc. Here is..."
categories:
  - "Company"
tags:
  - "arrow functions"
  - "callback"
  - "closure"
  - "php"
image: https://www.exakat.io/wp-content/uploads/2015/08/bell.320.jpg
word_count: 715
---

# The art of PHP callback

# The art of PHP callback

[PHP callback](http://php.net/manual/en/language.types.callable.php) are functions that may be called dynamically by PHP. They are used by native functions such as [array_map](http://php.net/array_map), [usort](http://php.net/usort), [preg_replace_callback](https://www.php.net/preg_replace_callback), etc.

Here is a reminder of the various ways to create a callback function in PHP, and use it with the native functions.

# String functions

Strings are the simplest way to dynamically call a function. The name of the function in the string is called. If the function is namespaced, you have to include the full namespace.

Note that some PHP native functions, such as 'echo' or 'print' are not really functions, so they can't be called directly with this approach. You have to put them in a custom function, and then, call this function.
Fatal error: Call to undefined function echo()

## Static Methods

Strings may also call a static method : simply add the class name and the `::` operator to call it. Class may be namespaced if needed.

Note that the method doesn't need to be static explicitely. If it isn't really static (aka, it does use `$this`), there will be errors as `$this` is null.

## Static Methods (Part 2)

It is possible to do the same call than the previous one with an array. The array must contain two elements : the class name and the method name. The index must be 0 and 1, respectively.

The same restriction applies to the `static` nature of the method.

Also, note that this syntax also allows the use of 'parent::' in the function's name, making it a call to the parent's class of the first argument.

## Normal Methods

The first argument may be replaced by a real object, instead of a string representing a class. This way, the object's method (and not the class) will be called.

## Callable object

A callable object is an object of a class that implements the[ magic method `\_\_invoke`](https://www.php.net/manual/en/language.oop5.magic.php#object.invoke). This method will be automagically called when the object is used as a callback.

Note that `__invoke` may be defined with an arbitrary number of arguments.

## Closures

[Closures](https://www.php.net/manual/en/class.closure.php) are functions without a name. Instead of providing a string that will reference compiled code, it is an object that may be called.

## Arrow functions

[Arrow functions](https://www.php.net/manual/en/functions.arrow.php) are closures with a single expression as body. They also bring into their context, all the local variables of the creating context, in a compact syntax. Arrow functions were introduced in

# create_function()

This is the grand-father of function creation. It is deprecated since PHP 7.2, and removed in PHP 8.0. It is highly recommended to *AVOID* using it, as it is a disguised version of `eval`. There are more modern ways to do this, so it is merely here in case you meet it in a legacy code.

It takes a list of arguments, and some PHP as a string, that will be `eval`'ed.

# Various ways to callback

No more than 7 ways to call a function or a method in PHP.

The modern way is definitely the closure. They are used heavily in frameworks to allow users to define a behavior and feed the framework with it. They are also non-polluting, since they don't need a specific name.