Return Types, they are a changin’

      No Comments on Return Types, they are a changin’

Thanks to the wonderful Levi Morrison, Andrea Faulds, Dmitry Stogov, and all of the hard-working men and women of the PHP Core team, PHP 7.1 will have void as a Return Type and nullable Return Types.

How We Got Here

In PHP 7.0, developers got a present in the form of method return types. It was originally it’s own RFC but before PHP 7.0 was released, it was rolled into the highly contentious Scalar Type Hints RFC. This gave us the option of specifying the data type the return of any given function or method. YEA! No more type checking return types. Life is good, right? Well mostly.

The problem is that when developers actually started working with this new feature in code that would be moving into production, they realized two critical things were missing.

  1. Sometimes a method that is supposed to return an integer might actually return a null, on purpose.
  2. Sometimes a method didn’t return anything, on purpose, and it needed to stay that way.

So those wacky women and men of the Core have introduced two new RFCs to correct these shortcomings of the existing Return Type feature of PHP.

PHP RFC: Nullable Types

Return Types are a great thing, until you don’t want them. The problem with the existing solution is you don’t have any option. If you specify an int, the method, and all of its overrides, have to return an int. Now I don’t mean options like “sometimes I feel like a string, sometimes I feel like an int”. No, Return Types solve that problem nicely. Bad things should happen to your code if your method says it will return an int and you instead return a string.

There are times, however, when even with your best intentions, you still may not know the right return value. As much as you may hate them, nulls serve an important function in programming, they say “I don’t know”. Sometimes that is the best we can do.

In 7.1 we can now specify that a return type may be an int or it may be a null.

The ? before the return type of int specifies NULL OR INT. Cool, huh? :)

 

PHP RFC: Void Return Type

Now that we can return a return value or null, let’s address the other hole in the current Return Type implementation, Void Return Types. If sometimes “I don’t know” is the right answer, other times just no answer at all is the right answer. Methods that simply do something and not return something are valid. It is, of course, possible to simply not specify a Return Type, this leaves future you reading the code wondering. Did you just forget, or is there truly no Return Type? We need some way of disambiguating things, now, we have it.

Now we can specify that a method returns void. This tells PHP that there should never ever be a return type from this method.

Correct:

Incorrect:

No matter what the method does, it can never return a value if you specify void as the return type.

:Conclusion

Return Types are a great new feature in PHP 7. I’ve used them (sparingly) in new code I’ve written. However, these two new additions will make it much easier for me to use them, and much easier for me to signal the intent of a method to other developers. (Including future me) If you are running PHP 7 on your servers, you should be rolling method Return Types into your code and you add new code to your application. If you aren’t running PHP 7 yet, well, you’ve got bigger problems than missing Return Types.

Update

Here is a video I recorded as part of Nomad PHP that is based on this article.

About Cal Evans

Many moons ago, at the tender age of 14, Cal touched his first computer. (We're using the term "computer" loosely here, it was a TRS-80 Model 1) Since then his life has never been the same. He graduated from TRS-80s to Commodores and eventually to IBM PCs.   For the past 10 years, Cal has worked with PHP and MySQL on Linux OSX, and when necessary, Windows. He has built on a variety of projects ranging in size from simple web pages to multi-million dollar web applications. When not banging his head on his monitor, attempting a blood sacrifice to get a particular piece of code working, he enjoys building and managing development teams using his widely imitated but never patented management style of "management by wandering around". Cal is happily married to wife 1.33, the lovely and talented Kathy. Together they have 2 kids who were both bright enough not to pursue a career in IT. Cal blogs at http://blog.calevans.com and is the founder and host of Nomad PHP