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

Anonymous Class

A class may be instantiated, without defining a class name. The class is identical to a named-class, except for the name attribute. This also means that it cannot be used for typing, though it may be typed itself, using its parent class or implemented interfaces.

Also, arguments may be passed at instantiation time, unlike with named-class definition, where those arguments are passed when calling new, not when defining the class.

<?php

    interface Pachyderm {}

    function trumpet(Pachyderm $a) {}

    $a = new class () implements Pachyderm {};

    trumpet($a);

?>

Documentation

See Also