PHP RFC: Structural Type Hinting

Introduction

Currently PHP uses an enforced interface style, where the argument object to a function or method must implement the respective interface. This RFC discusses inverting the dependency to allow for run-time resolving of interfaces. This is very similar to how GO implements interfaces.

In short, rather than checking the type-tree as traditional type-hinting works, this only checks to see that the public APIs match (that if you added “implements $interface” to the class definition, if it would pass)…

What Are Go-Style Interfaces?

Go-Style interfaces (called Structural Typing by this RFC) are basically interface type hinting which are resolved by looking at the structure of the object being passed rather than the type information. The class never implements the interface, but instead provides a compatible API. The receiver (the method receiving the argument) can choose to enforce the requirement or not.

Why Go-Style Interfaces?

The basic premise is that it makes two areas significantly easier to manage:

Cross-Project Dependencies

When you have cross-project dependencies. Currently, both packages must declare a dependency to a third package for the interface. A good example of this is the PSR-3 logger interface. Currently, the PSR-3 interface must be included by every project that declares a logger which you want to conform to the interface. This results in pulling in project-level dependencies even in cases where you don't need them.

Implementing Structural Typing would allow the Class maintainers to just build their classes without additional dependencies, but the receivers (consumers) of those objects to still have some form of type-safety.

Decoupling Of Classes

Right now, there's now way to type-hint on the PDO class while still allowing for decorators or other objects that also satisfy the same interface. Using Structural Typing that's completely possible. This should greatly simplify the building of Mock objects as well as generators.

Proposed Behavior

Any valid class-style identifier can be used as the “structure name”. That means that both classes and interfaces are supported as identifiers. Then the passed in object is checked to ensure that every method on the “structure type” matches in signature and flags to the passed object. If any do not match, the object is rejected and an E_RECOVERABLE_ERROR is raised.

Use-Cases

Flexible Middleware

Right now there's a project called Stack. The premise is to provide middlewares for Symfony's HttpKernel. In practice these middlewares are nothing more than decorators for the HttpKernel. Let's show the HttpKernel Interface:

The cool thing is that I'm effectively decoupled from Symfony here. If ZendFramework changed their Http\Client to use the same basic API, you could re-use the middleware on both, without needing a cross-project dependency between Symfony and Zend (and thereby loading the interfaces on every request.

Standards Based Interface Declarations

Currently, the PSR-FIG Group group is starting to publish interfaces for standardized components. At present, this requires that each project that either provides a “standard implementation” or uses a “standard implementation” must declare a dependency on this third project.

This raises a significant issue, because it causes a triangular dependency which requires some external effort to resolve. This means that you need some sort of tool to resolve that dependency for you, or you both sides copy/paste the implementation into their tree, and must “register an autoloader” for that dependency, and the first one to do so will win. Either way, it's not a straight forward solution.

So now, our code can depend on the narrower interface that we actually need, while allowing all PSR-3 compatible implementations to pass. But it also will allow us to replace polymorphically the logger with a non-PSR-3 logger (because it doesn't implement other parts of the interface, for example), but fulfills our entire need.

The key here is that it inverts the dependency on who gets to define what the needed units of functionality are. It allows the receiving code to define the scope of required functionality instead of the sending code.

It also solves the triangular dependency problem since the sender never needs to explicitly require the dependency. That can be left for an off-line check (or a test), reducing the amount of and need for dependency resolving tools for the purpose of common interfaces…

The Place For Current Interfaces

Why not just get rid of current interfaces and change their behavior to Structural typing (besides the MASSIVE BC break)?

In practice there are two reasons (times) that you would use an interface:

1. To provide typing information about a domain object (or a value object). This is where the typing actually means something specific to the application.

An example would be a User object in an application. You may want to have a UserInterface which the User class implements, because the interface actually implies that the object *belongs* to the domain. It's providing *type* information about the object.

2. To provide functionality information about a object. This where the interface really just describes the functionality of the object.

An example would be a service which encodes a password. There's no special typing information needed. The interface simply provides a semantic way of identifying the API of the service. So it's not really providing *type*, but more capability.

With this new proposal, Type information would still be implemented via traditional interfaces. But capability information would use Structural Typing.

So there is very much a place for both to live side-by-side in the same language.

Backward Incompatible Changes

Considering there is no addition to the reserved word table, and this only adds new branches to the compiler, there are no BC breaks.

Effects On Opcode Caches

The current implementation would have no effect on Op-Code caching.

Proposed PHP Version(s)

Proposed for PHP 5.NEXT

SAPIs Impacted

No SAPI impact.

Impact to Existing Extensions

There shouldn't be any Extension impact, as no APIs are changed. The only potential impact would be for extensions which are pre-processing the op-array prior to compiling, where the new operand type IS_PROTOCOL is used to signify the type-hint at the compiler level.

New Constants

None

php.ini Defaults

None

Open Issues

Raising Errors

Currently, only a E_RECOVERABLE_ERROR is raised saying that the passed object does not look like the structure type. We may want to raise an E_NOTICE or E_WARNING as well to say WHAT did not match (was a method missing? Was the signature different? etc).

Syntax

I considered implementing a new “type” of interface for this (declaring a new reserved word “structure”). However after thinking about it, I felt that it was necessary to extend this concept to classes as well as traditional interfaces.

If the <Foo> $foo syntax is not acceptable, there are a few alternatives that would likely work:

@Foo $foo

%Foo $foo

*Foo $foo

~Foo $foo

Personally, I think the <Foo> $foo syntax is the clearest, but it may be too close to Generics for comfort…

Performance

It's worth noting that since this is a separate branch, the current performance of normal type-hints remains uneffected.

Currently, instanceof short-circuiting and caching of structure checks has been implemented.

While performance is an apparent concern, the benchmarks indicate performance at works on-par with existing type hints (and when called multiple times can be faster)…