The static Return Type


This is a throwback to the Late Static Binding feature from PHP 5.3.0 and is going to make PHP classes that extend other classes and use strict types actually nice to work with for the first time. Here is a method from this blog's codebase for fetching something by its row ID. It is built into the core class that everything that uses database extends.

The code itself works fine, the problem occurs that when you use this method from a child class, your IDE's intelligent code bullshit will break. In the following example it is because it sees $Blog as an Atlantis\Prototype and not as an Atlantis\Prototype\Blog object.

There are two ways to "fix" this. With PHP 7.4 the way classes validate method overrides changed, so you can successfully override the parent method still using self and just directly call the parent version.

You cannot do this on older versions of PHP, like on 7.1 this will bomb with an error that the method override signature does not match the parent method signature due to self evaluating to Atlantis\Prototype on the parent and Atlantis\Prototype\Blog on the child. The other way to fix it would be to do this and it makes me want to launch into a long series of unkind statements.

Thankfully in PHP 8, the original parent method can just be defined to have a static return type instead of self, and this all of this, the override and the docblock, are totally not needed.


Annotations / Attributes


I hate "The PHP Docblock". For documentation my projects tend to use the Nether Senpai syntax which puts the content within the block of code it describes rather than outside, dissociated from the code it describes. The only time I have ever used The PHP Docblock is here on this blog project where the goal was to try and teach my JSON API's how to describe themselves by sending them a help command. In this case the docblock is being used just for adding metadata to the code.

To do this I used the Reflection stuff to scan for final methods in the API route, then read their PHP docblock with the Reflection stuff, then parse its contents, and dump it out.

At this point I am not entirely certain what my code will look like in the end but I am extremely excited to delete these docblocks and replace them with annotations directly connected to the method without the stupid /** * * * * * * * */ syntax.


Union Types


This will allow us to define multiple types for a single argument - which on the surface sounds like it might fly right in the face of and take a giant dump on the entire point of writing for strict types, but it will be very useful if you want to write a method that can take both Integers and Floats (aka, numbers) as an argument while still refusing literally anything else. Since PHP still doesn't have proper method overloading this will also be useful if you want to do like, int|float|string, do an is_int, is_float, or is_string check, and then call int, float, or string subversions of that method to do the actual work.


The str_contains function


This

becomes

enough said, lol.