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

Array Callback Index

<?php

class x {
    static function foo() { echo __METHOD__.PHP_EOL;}
}

['x', 'foo']();
[0 => 'x', 1 => 'foo']();
//Array callback has to contain indices 0 and 1
//[10 => 'x', 11 => 'foo']();
//Array callback must have exactly two elements
//[10 => 'x', 11 => 'foo', 0 =>0, 1=>1]();

// OK! 
[1 => 'foo', 0 => 'x']();

An array callback must be built with two elements, and their index must be 0, for the class or object, and 1 for the method name. PHP enforces the number of elements in the array, and the actual used index.

On the other hand, one may build the array in the wrong order but the correct index, and get the expected result. Nice!

See Also

PHP Error Messages

PHP Features

Last updated: 09 November 2026