PHP 7.0 - Anonymous Classes
A new feature of php 7.0 is anonymous classes. Anonymous classes are useful when simple, one-off objects need to be created, such as a value object. Below is an example of using an anonymous class for ensuring a user provided string is valid, and converts it for later use.
Remember that you have created an object, rather than a class definition, so you couldn't do this:
Another possible use for anonymous classes is to create a one off object that implements an interface for dependency injection rather than having to create a whole new class file somewhere. E.g.
Last updated: 16th September 2021
First published: 16th August 2018
First published: 16th August 2018