PHP RFC: Anonymous Classes

Introduction

For some time PHP has featured anonymous function support in the shape of Closures; this patch introduces the same kind of functionality for objects of an anonymous class.

The ability to create objects of an anonymous class is an established and well used part of Object Orientated programming in other languages (namely C# and Java).

An anonymous class might be used over a named class:

when the class does not need to be documented

when the class is used only once during execution

An anonymous class is a class without a (programmer declared) name. The functionality of the object is no different from that of an object of a named class. They use the existing class syntax, with the name missing:

Reflection

The only change to reflection is to add ReflectionClass::isAnonymous().

Serialization

Serialization is not supported, and will error just as anonymous functions do.

Internal Class Naming

The internal name of an anonymous class is generated with a unique reference based on its address.

function my_factory_function(){returnnewclass{};}

get_class(my_factory_function()) would return “class@0x7fa77f271bd0” even if called multiple times, as it is the same definition. The word “class” is used by default, but if the anonymous class extends a named class it will use that:

class mine {}newclassextends mine {};

This class name will be “mine@0x7fc4be471000”.

Multiple anonymous classes created in the same position (say, a loop) can be compared with `==`, but those created elsewhere will not match as they will have a different name.

Both classes where identical in every way, other than their generated name.

Use Cases

Code testing presents the most significant number of use cases, however, where anonymous classes are a part of a language they do find their way into many use cases, not just testing. Whether it is technically correct to use an anonymous class depends almost entirely on an individual application, or even object depending on perspective.

This saved us making a new file, or placing the class definition at the top of the file or somewhere a long way from its usage. For big complex actions, or anything that needs to be reused, that would of course be better off as a named class, but in this case it's nice and handy to not bother.

If you need to implement a very light interface to create a simple dependency:

Anonymous classes do present the opportunity to create the first kind of nested class in PHP. You might nest for slightly different reasons to creating an anonymous class, so that deserves some discussion;

Note: Outer is extended not for access to $this→data - that could just be passed into a constructor; extending Outer allows the nested class implementing ArrayAccess permission to execute protected methods, declared in the Outer class, on the same $this→data, and if by reference, as if they are the Outer class.

In the simple example above Outer::getArrayAccess takes advantage of anonymous classes to declare and create an ArrayAccess interface object for Outer.

By making getArrayAccess private the anonymous class it creates can be said to be a private class.

This increases the possibilities for grouping of your objects functionality, can lead to more readable, some might say more maintainable code.