News Archive
PhpRiot Newsletter
Your Email Address:

More information

Scalar type hinting is harder than you think

Note: This article was originally published at Planet PHP on 6 March 2012.
Planet PHP

One of the features originally planned for PHP 5.4 was scalar type hinting. But as you know, they weren't included in the release.

Recently the topic has come up again on the mailing list and there has been a hell lot of discussion about it. Yesterday ircmaxell published a blog post about his particular proposals.

The reactions on reddit were mixed. On one hand it is clear that people do really want scalar type hints, on the other hand they didn't seem to like that particular proposal.

One comment particularly caught my interest:

Why can't the PHP dev's just get this thing right? Why must they always choose the worst, more useless, most convoluted way to implement every new feature! It's disheartening. Especially when there aren't good backwards-compatibility reasons for doing it wrong.

In a way, this is a pretty important comment: It shows what people think about how PHP is being developed. They think that it's a bunch of people that somehow, magically, manage to always do the wrong decisions.

That's not how it works. Really. You see, most people, when confronted with scalar type hinting, think aoHey, this is so simple, just do XYZa, and then wonder why PHP is having such a hard time implementing this most trivial of all features.

The thing is: It's just not that simple. Most proposals seem so easy and straightforward on first sight, but when it comes to the details they are not.

In the following I want to introduce you to some of the various proposals that were made in the past and the problems associated with them. I hope this way people will get a little bit more insight into why this is so hard.

Strict type hinting

I'll start with the proposal that I personally dislike most: Strict type hinting, i.e. allowing only the hinted type to be passed and not any of the types that PHP would normally consider equivalent. See this example:

function foo(int $i) { /* ... */ } foo(1); // works foo(1.0); // fatal error: int expected, float given foo("1"); // fatal error: int expected, string given

I think it is evident that this is not an option. One of PHP's greatest strengths is being a weakly typed language and this proposal would turn it into a strictly typed one. This goes against the PHP philosophy and also differs from how pretty much everything else works in PHP.

Unenforced type hinting

Another proposal that was made, is type hinting that isn't enforced by the engine. WTF? What would this be good for? Basically, it would work just like doc comments (which aren't enforced either ^^), but with nicer syntax.

function foo(int $i) { /* ... */ } foo(1); // works foo(1.0); // works foo("1"); // works foo(array(123); // works foo($iAmObject); // works // everything works...

I dislike this proposal, too, for

Truncated by Planet PHP, read more at the original (another 14578 bytes)