Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Callable Arrays

An callable array is an array with index 0 and 1. The first index is a string, that represents a valid class, or an object. The second argument is a string, which is a method name.

With such a configuration, it is possible to use that array as a callback: either with a static method call, or a normal method call.

<?php

class Elephpant {
    static function stampede() { echo __METHOD__;}
           function trumpet() { echo __METHOD__;}
}

$callable1 = [new Elephpant, 'trumpet'];
$callable1(); // Elephpant::trumpet

$callable2 = ['Elephpant', 'stampede'];
$callable2(); // Elephpant::stampede

$callable3 = [Elephpant::class, 'stampede'];
$callable3(); // Elephpant::stampede

?>

Documentation

See Also