Anonymous Classes

Anonymous Class

Anonymous classes are the classes which are defined without any name. In PHP 7, we can define an anonymous class using keyword 'new class'. This class can replace a full class definition.

PHP 7 has introduced a new class feature called the Anonymous Class which allow us to create objects without the need to name them.

<?php
$foo = new class() {
  public function bar() {
     return "bar";
  }
};
echo $foo->bar();
?>

Output:

bar