Five Exciting Features in PHP 8
Time to read: 5 minutes
It's getting close. PHP 8 will release on November 26th 2020, and I, for one, couldn't be more excited. Equally exciting was that June marked the first alpha release of PHP 8.
I've been playing with PHP 8 since Derick Rethans introduced me to the features on a live stream and I'm hooked on the shiny new features we're getting. So here, months in advance are the 5 things I'm most excited about in PHP 8.
Disclaimer: only features actually implemented are in this list, I'll likely make another list later with the things that aren't created yet.
Constructor Property Promotion
Let's be clear, these are in no particular order, but long time readers/viewers will know that I have a certain unique level of disdain for boilerplate. One thing we do as PHP developers is create value object classes that only contain data values. Still, even when we're creating intricate services, they often have multiple dependencies. Whatever we’re building, we end up writing constructor initialisation over and over again.
Constructor property promotion removes this tedium by allowing us to declare these properties and set them in one place instead of three; in the constructor parameters.
https://3v4l.org/kKtYs
We can preface the type declaration with property visibility if we want a property created and initialised for us with the value passed in, automatically. In this case, the `name` property will be available privately and set to whatever string we pass in as the second argument.
There are some caveats and rules around how and when you can use promotion, which is explained well in this blog post. I'm so happy to remove more boilerplate from my code, now we just need to find a way to get rid of those annoying getters and setters.
Union Types
Type declarations are the best. Since we got proper scalar type declarations and return types in PHP 7, through to PHP 7.4's property types, PHP's type system gets stricter and stricter. For those of us who love that kind of thing, it's a joy. PHP 8 takes 2 more steps forward and maybe one step back with the introduction of union types.
I've been doing a lot of open source work modernising the Service Manager from the Laminas (previously Zend Framework) project. Part of my work has been adding type declarations across the board, and it's made a remarkable difference. Not only have we removed a ton of unit tests, but the code has become self-documenting too, which is a great win.
One annoyance while doing this is because of past breaking changes and the lengthy history of this project, in some cases, single types are just not possible to add.
Here we can see that the setFactory
method can take several types for a $factory
and that's fine. In an ideal world, we'd refactor this to create new methods each taking a single type that calls this generic method under the hood, then make this method private, but that introduces a backwards compatibility (BC) break. It's also a very time-consuming amount of work to clear with management when you're under pressure to release the next impressive feature.
Union types can give us a measure of type safety in these exact situations where legacy code has deemed it impossible to receive or return a single type.
This isn't ideal, we should be cautious when adding union types to new code as they can be a decent indicator of bad API design. Still, for legacy code improvements this is a useful improvement.
Attributes
DocBlock annotations have become an everyday part of PHP development, and they are great. Lots of annotations have become de-facto standards that improve functionality in our IDEs, like @var
and @deprecated
. Libraries like Doctrine Annotations allow us to parse these docblocks at run-time to add all kinds of swanky new magic functionality based on their values.
But it's a little like the wild-west out there. Annotations starting with an @
are by convention only, and your docblocks are comments and therefore not subject to parse errors or other type safety.
Say <<Hello>>
to attributes.
Attributes are a formalised standard way to add metadata to your PHP code, parsed by the PHP engine and available in reflection. They are currently surrounded with <<
and >>
and work very similarly to how annotations do, but are a first-class construct of the language.
It's difficult to say just how these will change the landscape of PHP in the coming years. Without using these in anger in real projects, it's challenging to comprehend where this will take us. As a community, we'll still need to agree on standards and conventions for using attributes just like we did with docblocks, and tools like IDEs and static analysis libraries will need to start supporting them. At least the caching and parsing of metadata can now be handled by the PHP engine. Let's wait and see.
NOTE: As of 1st July 2020 the attribute format has changed from `<<>>` to `@@` thanks to this RFC
String Functions
If you haven't implemented your own stringContains
function you're either very new at PHP, using a framework, or a liar. I've also written my own stringStartsWith
and stringEndsWith
functions more than once over the years.
PHP 8 brings us these functions as part of the language. Hardly exciting, but a solid upgrade nevertheless.
One annoying thing for me at least is that the argument order is $haystack
then $needle
to stay in line with strpos
. It's a small thing, but I guess we should rejoice in this being one of those cases where consistency is better than logic.
Stringable Interface
Another relatively minor but useful addition, the Stringable interface allows you to type on something that is castable to a string via the __toString
magic method. Previously it wasn't possible to declare a type that was a string
or Stringable
, you'd have to do some shenanigans with is_callable
or reflection.
PHP 8's Stringable
interface allows you to type hint on something that implements __toString
and is applied automatically to any class that implements that method.
Bonus Round
Did you notice anything slightly different in the first PHP 8 constructor property promotion example? Look carefully on line 13, you'll see we've left in the trailing comma for the last parameter in the list. This isn't a typo, trailing parameters are now allowed which brings parameter lists in line with arrays in this regard. Hoorah!
What features are you most excited about for PHP 8? I haven't mentioned the JIT compiler just yet because I'm unsure how much gain it will give in real-world codebases. Let me know what you think on Twitter or in the comments below. We'll be sure to re-address PHP 8 in the future as the release date gets closer.
I can't wait to see what you build.
- Email: ghockin@twilio.com
- Twitter: @GeeH
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.