It’s been a while in coming, OK it’s still not quite here yet, but the next version of PHP, PHP 7, is expected to released in October this year. Now if you’re out of the loop, this might seem strange, going from version 5.6 to 7. But well, PHP 6, yeah, that just never quite happened.
So, to make a clean break from all the controversy, delays and so forth around version 6, the contributors are moving straight to version 7. Given we’re in July, and October is right around the proverbial corner, I thought it’s time, if you’re not up to speed already on what’s coming, to have a look at some of the key changes which you can expect in version 7, of one of the world’s most widely used programming languages.
Note: The second alpha release of PHP 7 is now available.
Performance
As an early(er) adopter of PHP, I always remember it was, quite often, much faster than other web languages. But that hasn’t always stayed true, over the course of the years. However in the next release, if the speed wasn’t there already, it’s definitely set to return. According to Zend, depending on application architecture and hardware constraints, performance is set to increase anywhere from 25 to 70%, which is truly remarkable.
Similar to the effort involved in speeding up an application by just enabling an APC cache, no code refactoring is required to get this speed-up. All that’s required is a version upgrade. Where else can you get such a performance boost for so little effort?
The reason for the speed-up is because version 7 is based on phpng, which had two key goals: reduction in memory consumption and increasing performance. The phpng RFC indicates even further performance improvements than the official Zend documentation, of between 20 and 110%. So no matter which way you look at it, you’ll be able to do so much more with your existing hardware, whether that’s improving the performance of an existing application, or hosting more applications on the same amount of hardware.
Scalar Type Hints & Return Type Declarations
This is one change which I’ve been looking forward to, with much anticipation, for some time now. If you’re not familiar with them, scalar type hinting extends the existing function type hinting support to PHP’s scalar data types. Type hinting was first introduced in PHP in version 5, but was limited to classes, interfaces, arrays, and callables.
Return type hints takes the concept further, allowing developers to specify the expected variable type which the function will return. This is similar to such languages as C, C++, Java, and Go. You don’t have to use either of them but, if you do, they’ll make your code clearer, and easier to read.
Whether that’s readers who peruse your code, or to the assistance that popular IDEs, such as PHPStorm and Netbeans, provide, when type hinting is used. Here’s some example code, so that you can see what it’s like, if this is your first time encountering type hinting.
php function sumTwoInts(int $numOne, int $numTwo): float { return (float) $numOne + $numTwo }
For the most part, this function is just like any other function in PHP, whether that’s a standalone function, or a member function of a class. All it does is return the sum of the two numbers provided, explicitly cast to a float.
Note that type hinting is provided for both arguments, meaning that both can only be integer variables. Then, after the function arguments, note the : float. This indicates to the compiler that the function can only return a float. Now we could have written it as follows:
php function sumTwoInts($numOne, $numTwo) { return (float) $numOne + $numTwo }
Both would have achieved the same affect. However, with version two, we’d likely have had to have added in some type checking. What’s more, code that uses the function may have had to have also done some type checking of the return value. Scalar and return type hints offsets that effort to the compiler, reducing the amount of code needed to be written.
Spaceship Operator
This is a change which, whilst not controversial, has received a mixed response. Dubbed the “spaceship operator” because it looks like a Star Wars Tie Fighter, the operator provides similar functionality to the strcmp() and version_compare() functions. Quoting the PHP manual:
It returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater. It uses exactly the same comparison rules as used by our existing comparison operators: <, <=, ==, >= and >.
This new operator is able to be used on all of PHP’s generic types. Here are some examples of using it, along with the returned result.
php echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1
Remove PHP 4 Style Constructors
This is a change I’m all too happy to see. In PHP 4, a constructor was a function with the same name as it’s encompassing class, such as in the following example.
php class MyClass { // class constructor public function MyClass(){} }
However, when you move to PHP 5, things get more than a bit strange. Here’s why:
- If you namespace a class, it doesn’t recognise PHP 4 style constructors
- If you define a PHP 5 constructor __construct, and a function with the same name, disables PHP 4 constructors.
- Defining a PHP 4 constructor first, then a PHP 5 constructor raises an E_STRICT error
See the problem? Allowing this kind of code pollution to continue raises all sorts of potential logic errors. So removing this functionality altogether is a worthwhile break. If you have a codebase which makes heavy use of PHP 4 constructors, I appreciate that it may take a while to refactor. But for consistency’s sake, it’s a task worth undertaking.
Unicode Codepoint Escape Syntax
For the longest time, despite PHP being such a global language, its support for Unicode hasn’t been all that great. Now this may not be important to you. But arguably, your audience is going to be quite limited if that’s your point of view, as standard ASCII can only support a very limited number of characters.
Given that there are, at the time of writing, over 6,500 spoken languages, with all their characters, such as the umlauts in German, the French accents, and let’s not get in to the always growing range of emoji’s, this change is needed.
But what exactly is it? The Unicode codepoint escape syntax provides allows you to specify Unicode codepoints by number, in a string, such as in the following example, much the same way you’d express an octal or hexadecimal number.
php echo "u{202E}Reversed text"; // outputs txet desreveR
Not So Significant Changes
Now let’s look at some less significant changes, ones which may or may not affect you.
ASP and Script Tags Have Been Removed
PHP has supported a wide range of script tags, in addition to it’s standard longer syntax; i.e., <?php. These were ASP style, such as <% and the short tag syntax <%= %>, and the script style, such as <script language='php'>…</script>. In PHP 7, all of these alternate syntaxes are removed.
I can’t speak for it myself, but one of the key reasons proposed for removal of these alternate tag syntaxes is a lack of uptake and a clash with newer languages, such as underscore.js. Given the first point at least, the odds of you missing this feature is quite minimal. However, before you upgrade, it’s worth checking your code and templates, to ensure that you’re not using them.
Removal of Deprecated Functionality
Over the course of PHP 5.x a range of functionality’s been gradually deprecated. In PHP 7, a lot of that deprecated functionality is now being, officially, removed. Two such areas are POSIX compatible regular expressions (ereg) and the old MySQL extension.
In PHP 5.3 POSIX compatible regular expressions, the ereg extension, was deprecated. In PHP 5.5 the old MySQL extension was deprecated. In PHP 7, they’re gone. If you need them, at least whilst you’re transitioning, they are available as PECL extensions.
How To Get Started Now
So, do you want to get your hands on PHP 7 and start testing it out, as well as testing how compatible your existing codebase is with it? Great. But how do you do it? Well, depending on your platform, there are a number of ways to get started.
Compiling From Source (alpha release)
To compile from source, download the latest alpha release, then run the following commands, assuming that you’re on a Linux/UNIXtm system:
bash ./configure make make install
Mac OS X
If you’re on Mac OS X, you can use the Homebrew package to install it. There’s no official tap available, as the lead developer didn’t see the point yet, as PHP 7’s not yet stable. However a user contributed tap is available. To use it, run the following commands:
bash sudo brew tap guoxiao/homebrew-php7 sudo brew install --HEAD php7
Once installed, you’ll find it in /usr/local/Cellar/php7/HEAD/bin.
Linux
If you’re on Linux, you can build PHP 7 from source, by running the following commands:
bash git clone https://git.php.net/repository/php-src.git cd php-src ./buildconf ./configure make && make install
Windows
I’m not a Windows user, but here’s a detailed post from Adam Cameron, detailing how go get PHP 7 alpha running on the Windows platform.
Wrapping Up
And that’s a wrap on some of the key features which you can expect in PHP 7, due out around October this year. There’s a lot more coming, but these have been some of the key, sometimes most controversial, features of the next release.
There’s so much going for it, that it’s easy to get excited about what’s coming. Whilst it will take time to see a proper uptake and adoption of the new version, there’s no time like the present to start experimenting with it, and considering migrating your application to it.
More Information
- https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7
- https://www.sitepoint.com/php-7-revolution-return-types-removed-artifacts/
- https://www.infoworld.com/article/2841561/php/php-7-moves-full-speed-ahead.html
- https://www.phpclasses.org/blog/post/242-PHP-7-Features-and-Release-Date.html
- https://php.net/manual/en/language.oop5.typehinting.php
- https://en.wikipedia.org/wiki/Return_type
- https://wiki.php.net/rfc/remove_php4_constructors