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 Was Customizable

<?php

$anonymous = new class () {};

class_alias($anonymous::class, \array::class);

function foo(\array $object) : \array {
    var_dump($object);

    return $object;
}

foo($anonymous);

/*
object(class@anonymous)#1 (0) {
}
*/
?>

This code gathers several PHP hacks together.

First, the naming of an anonymous class, and this time, it is renamed as \array.

array would not work, but the absolute naming make it pass.

Then, that \array is used as type, and it works.

Now, there is something that looks like an array, but is actually a random object.

This is a bug, since other PHP native types, like int disallow usage of their absolute name.

This was reported as a bug, and fixed in PHP 8.4. Thanks!.

See Also

PHP Features

Last updated: 09 November 2026