There are some advantages:
Before understanding its vantages, it is also necessary to understand the importance of anonymous methods (closures), such as the methods that allow callback (ie, the return of something already expected), the concept is the same for classes.
Such as closures, anonymous classes are useful when just created and / or used at runtime:
<?php
var_dump((new class {
public function execute() { return 12345; }
})->execute()); // 12345
Another advantage is when we use several classes of the same namespace , now you can group them instead of repeating the namespace for each class, as follows:
Before:
<?php
// PHP 5.6
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
?>
After:
<?php
// PHP 7
use yii\helpers\{ArrayHelper, Html, Url};
?>
Anonymous classes can be imitated (to some extent, at least) with relative ease. It also allows us to callback implementations for your methods dynamically using closures . Besides doing all the other things an ordinary class already does.