Mark Baker, serial conference speaker and creator of the PhpSpreadsheet package shares some thoughts on how to create private classes using anonymous classes.

I’ve written before about the benefits of using PHP’s Anonymous Classes for test doubles; but Anonymous Classes also have potential usecases within production code as well. In this article I’m going to describe one such usecase that can be particularly useful within libraries, and that is replicating the access of Package Private (in Java), or Protected Internal Classes (as per C#).

https://markbakeruk.net/2018/06/25/using-php-anonymous-classes-as-package-private-classes/