PHP Annotated – August 2020
Greetings everyone,
Please welcome the August installment of PHP Annotated!
PHP 8 has reached feature freeze and received a few last big additions: the nullsafe operator, named arguments, and saner string to number comparison. The syntax for attributes, though, has not yet been decided. Read the full story behind the attributes from the initial proposal to the latest revote and the battle between #[]
and @[]
. And as usual, we’ve got a load of useful articles, tools, videos, and podcasts for you in this issue.
⚡️ News & Releases
- PHP 8.0.0 Beta 1 — With the first beta release, PHP 8 has reached a feature freeze, which means that the list of changes in this branch has been settled and can be considered final… except for the syntax of attributes. The next Beta is scheduled for August 20.
Meanwhile, check out the overviews of all new features from Brent Roose or from Ayesh Karunaratne, as well as PHP 8: before and after. There is also a video overview, PHP 8: Process & fixing insanity, from one of the core contributors, George Peter Banyard. Other similar PHP 8 overviews by Nikita Popov and by William Pinaud are available, too, as well as an audio interview with the PHP 8 release managers Sara Golemon and Garbriel Caruso in the Voices of the ElePHPant podcast.
- PHP 7.4.9, 7.3.21, 7.2.33 — Security updates for supported branches with a fix for this vulnerability in PHAR.
- WordPress 5.5 “Eckstine” — This release finally brings auto-updates for themes and plugins to help make CMS installations safer.
- PhpStorm 2020.2 released — PHP 8 union types, a new control flow engine for PHP, GitHub pull requests, the Extract Class refactoring, and many other features were added in this release. Visit the what’s new page to learn more.
🐘 PHP Internals
- 🍿 [RFC] Shorter Attribute Syntax Change — The syntax for attributes in PHP 8 is still not decided yet.
The syntax for attributes in PHP 8 is still undecided. First, Benjamin Eberlei proposed
<< >>
and it was accepted. Later there was voting for a new syntax with 3 options:<< >>
,#[]
, and@@
. The latter won, probably as it is the one most similar to PHPDoc tags and Java annotations. However, it turned out @@ has some parsing issues, as well as a bunch of other problems.The
#[Attr]
syntax has some considerations too: 1, 2.And now it’s been decided to have another revote to choose from one of the following syntax options:
@@Attr
,#[Attr]
,<<Attr>>
и@[Attr]
. Reactions from the community have been in good humor:Anyway, there’s no need to worry because we’ll get used to any syntax… eventually.
Anxious about the outcome of the Great PHP Syntax Bikeshed of 2020 aka @[#<Attributes}:)? You don't need to be. The Great Bikeshed of 2008 on namespace separators was quite similar, and in the end we all grew used to it and moved on. https://t.co/XKG6UM5uJ1 #phpc #oldpeoplewisdom
— Jordi Boggiano (@seldaek) August 11, 2020
It’s hard to believe that back in 2008, instead of
\
for the namespace separator people considered**
,^^
,:>
,:)
,:::
, and%%
.The choice everyone wanted was :: as in C++ but it was not possible. The problem was found late as implementation with :: was already in place afair
— Benjamin Eberlei (@beberlei) August 13, 2020
Well, there is always a plan B to satisfy everyone and no-one at the same time.
New proposal – PHP 8 Annotations / Attributes should be:
@@#[<< Annotation >>]
There, now everyone has what they want.
— Chris Emerson (@chris_emerson) July 28, 2020
- ✅ [RFC] Saner string to number comparisons — The proposal that breaks backward compatibility passed almost unanimously.
In PHP 8, when comparing numbers and strings with non-strict
==
, both operands will be converted to a string and compared as strings if any of them is not a numeric string.0 == 'foobar'
is now officiallyfalse
.It also entails a change in the behavior of all comparison operators:
<=>
,==
,!=
,>
,>=
,<
and<=
, theswitch
construct, and functions such asin_array()
,sort()
and others. - ✅ [RFC] Treat namespaced names as a single token — In PHP 8, the entire namespace is considered a single token. This allows keywords to be used within the namespace, for example,
namespace app\function { class Foo {} }
and avoids potential backward compatibility issues when introducing new keywords.With this change, namespaces can no longer contain comments, which means anything this ugly should never see the light of day again:
use /** Try comments */ \FullyQualified \ /* in this ugly way */ SometTotallyDifferentTrait /** also after */;
- ✅ [RFC] Nullsafe operator — The new operator
?->
has been accepted for PHP 8.
Instead of lots of null checks, you will be able to write elegant one-liners such as$country = $this->sessionStorage->getSession()?->getUser()?->address?->country;
- ✅ [RFC] Named Arguments — PHP 8 will have named arguments/parameters! It will now be possible to pass values to a function or method based on the parameter name, not just its position.
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401 , ini_get("default_charset"), false);
will become:
htmlspecialchars($string, double_encode: false);
Learn more about named arguments in the post stitcher.io/blog/php-8-named-arguments.
- [RFC] Named Parameters explicit opt in — The document has been redesigned and now, instead of renaming the parameters, it proposes only to explicitly indicate that the parameter is a named one.
Since PHP 8 has already reached the feature-freeze stage, this proposal may only come about in 8.1. - [Proposal] Bigint shorthand (123n) for GMP objects — A very interesting proposal for the next versions, with no official RFC so far.
Tyson Andre proposes to implement a syntax for large numbers by adding an "n" suffix, similar to how it’s done in JavaScript:
$theBiggestInt = 9007199254740991n
Under the hood, this will create GMP objects and will allow us to use them just like normal numbers in PHP, because the arithmetic, bitwise, and comparison operators are already overloaded for GMP objects.
🛠 Tools
- PHPUnit 9.3 — A fresh update with support for PHP 8 and Xdebug 3.
- hidehalo/nanoid-php — PHP implementation of Nanoid, secure URL-friendly unique ID generator.
- woohoolabs/zen — Fast and simple, PSR-11 compliant DI Container and preload file generator.
- loophp/collection — A (memory) friendly, easy, lazy, and modular collection class.
- ramsey/conventional-commits — A PHP library for creating and validating commit messages according to the Conventional Commits specification. Includes a CaptainHook plugin.
- jubianchi/ppc — A parser generator library for PHP. As an alternative check out phplrt/phplrt.
- zakirullin/mess — A wrapper for scalars and arrays for type-safe casting.
- nette/php-generator — HP code generator with a fluent interface.
Symfony
- Using Symfony’s service iterators for secondary flows.
- Deploy a Symfony application in AWS Lambda.
- A Week of Symfony #711 (August 10-16, 2020)
-
As of today, #Symfony 4.3 and 5.0 are out of maintenance \o/
It's time to move on to 5.1 if you didn't already! pic.twitter.com/PdwNzRrY7O— Nicolas Grekas (@nicolasgrekas) July 24, 2020
Laravel
- A nice guide for converting a legacy application to Laravel.
- Running The Same Queued Job Multiple Times.
- 📺 Don’t use mass assignment protection in your Laravel models.
- 📺 Better Laravel Way — A few videos on the framework on different topics.
- 📺 A stream with Nuno Maduro and Freek Murze where they convert PHPUnit tests to Pest.
- 📺 Laracon EU Online 2020 videos.
💡 Misc
- Testing Microservices in PHP with Codeception.
- Antipatterns of legacy PHP applications.
- On the method fossilisation problem.
- A case for system-wide dependency injection in PHP.
- Friendly classes in PHP — There was an RFC on the subject, but it did not pass.
- Everything you need (and don’t need) to know about PHP’s type system.
- Enhancements to PHP code coverage in 2020 — Read about metrics, branch coverage, and path coverage.
- PHP: Preloading – Manual — Detailed documentation on preloading, which was added in PHP 7.4, is finally available.
- PhpStorm, But Better – Tips & Tricks Newsletter — Two emails already sent: #1, #2. Sign up to not miss the next one.
- Are you sure you write OOP code?
- Order of evaluation in PHP — An old but very interesting write-up by Nikita Popov: “Operator precedence and associativity specify grouping, but they do not specify in which order the groups are executed”.
📺 Videos
- 📺 Type Juggling Magic: Why PHP thinks 0 and “password” are the same — A funny video on how type casting in PHP can lead to security problems.
- 📺 Stream with Derick Rethans on how to work with Xdebug — Setting up Xdebug with Laravel Valet and PhpStorm; covers profiling, debugging, and other tips.
- 📺 Dutch PHP Conf 2020 videos.
🔈 Podcasts
- PHP Internals News Podcast by Derick Rethans
• #63 — With André Rømcke on property write/set visibility.
• #64 — With Benjamin Eberlei on everything about attributes.
• #65 — With Dan Ackroyd about the new?->
operator in PHP 8.
• #66 — With Nikita Popov on single token for namespaces and parsing PHP in general.
And last but not least, you can now get original Blue or Pink elephpants from their designer Vincent Pontier on www.elephpant.com. Gray and Black ones are also available at shop.symfony.com with worldwide shipping.
Thanks for reading!
If you have any interesting or useful links to share via PHP Annotated, please leave a comment on this post or send me a tweet.
Your JetBrains PhpStorm team
The Drive to Develop