8 cool features to come in PHP 7.1
PHP communities are working hard to keep the atmosphere pleasant. Programming is fun and enjoyable and when a programming language makes development easy, coding becomes love.
Not long ago the latest major version of PHP was released, the version 7.0. Even though a lot has been done to fulfill that task, still the community has not stopped working to keep improving our **lovely programming language.
In this post, I will share with you some 8 cool features that have already been implemented in PHP 7.1. Many RFCs are still undervoted. Probably many are still to come. On the other hand, there are still many people who haven't migrated to PHP7. This is normally a big deal but you must know that soon the 5.5 and 5.6 will get to their end.
Have you still not migrated to PHP7?
— Justin Baker (@phpmvp) June 2, 2016
- PHP5.5 security support ends July 10th
- PHP5.6 active support ends Dec 31st
??UPGRADE NOW ??
Now let's have a look at the new features we'll be enjoying in the next 7.1:
1. Nullable types
In the first release of PHP 7 the return type declaration, was made possible. This means you could precise which data type can your function return like this:
<?php
function add($a, $b) : float{
return $a + $b;
}
This function will always output a float
. But the issue is null
too is a type but often used to represent the absence of an explicit type. For example, it's used to represent function default parameters. Usually, you have to test if a returned type/variable is null or not, like this:
<?php
function test($param = null)
{
if(!is_null($param))
{
return $param;
}
return null;
}
To avoid such cases, we can use the nullable type like this:
<?php
// We insist the returned type should be int
// otherwise it will be null by default
function answer(): ?int {
return null; //ok
}
function answer(): ?int {
return 42; // ok
}
function answer(): ?int {
return new stdclass(); // error
}
Read more on this feature here.
2. Square bracket syntax for array destructuring assignment
I think there is no need to talk much about this one since I've dedicated an entire article for it some days ago. Read it here.
3. Warn about invalid strings in arithmetic
This feature will allow us to have E_NOTICE
or E_WARNING
every time a string is used in any arithmetical operation like this:
<?php
$numberOfApples = "10 apples" + "5 pears";
In case you do this, an error like the following will be thrown:
Notice: A non-well-formed numeric string encountered in example.php on line 3
Notice: A non-well-formed numeric string encountered in example.php on line 3
Or if you do this:
<?php
$numberOfPears = 5 * "orange";
You get the following warning:
Warning: A non-numeric string encountered in example.php on line 3
You can read more about this feature here.
4. Allow specifying keys in list()
I made mention of this here too. This works with the array destructuring
. It will allow us to specify key
in the arrays
while destructuring them with list()
function which wasn't possible before.
Read more about it here.
5. Generalize support of negative string offsets
This is a feature that was voted by unanimity. It's simply useful. If you've faced a situation whereby you need to access a character at a position in a string, you should know PHP was not making it easy to handle. Let me explain this by using a common example of the substr() function. In this function you could specify a negative
number for the start parameter, then the function will look for the occurrence from the end of the character. If it's a positive number it starts from the beginning:
<?php
$rest = substr("abcdef", -1); // returns "f"
$rest = substr("abcdef", -2); // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
But when you look at strpos() which finds a numeric position of the first occurrence of needle in a string. Its offset doesn't accept negative values.
So, this feature will now allow us to use negative offsets. Not only with these two functions but any function require an offset or a position, even the string access to individual characters can accept negative values using []
or {}
like this: $str = 'abcd'; echo $str[-1];
Read more about it here
6. Void Return Type
PHP has started to be worried about its data types and this new version has considerably done a lot of work about it. We know we can return types now, besides we can precise nullable type. What of nothing at all? As you may know it, in most programming languages like C
or Java
there is void type to say, there is nothing to return. Exactly! we can do the same thing now with this feature in PHP 7.1. All we'll have to do is to use the returning type syntax and instead of using a data type keyword like int
, float
, etc will use void
instead:
<?php
function lacks_return(): void {
// valid
}
function returns_nothing(): void {
return; // valid
}
function returns_one(): void {
return 1; // Fatal error: A void function must not return a value
}
function returns_null(): void {
return null; // Fatal error: A void function must not return a value
}
Read more about this feature here
7. Support Class Constant Visibility
Yes! I am excited like many programmers in PHP, who have been wondering why couldn't we specify the constant
visibility in classes. Relax, this is now implemented. You can do it in classes and interfaces.
For those who don't understand this is about yet let me explain this a bit. In classes you can specify the visibility of a method
or a property
like this:
<?php
// property with **private** visibility
private $key;
// a constant
const CONSTANT_A = 'A';
// method with **public** visibility
public function display()
{
}
Note the CONSTANT_A
doesn't have any visibility such us public
, private
, or protected
. Now from PHP 7.1, we can do it. That's what I am talking about :-)
Read it all here
8. Catching Multiple Exception Types
This is a tiny feature but which will save a lot of headaches when it comes to handling many exceptions of different types. Let me pick a typical example from the RFC:
<?php
try {
// Some code...
} catch (ExceptionType1 $e) {
// Code to handle the exception
} catch (ExceptionType2 $e) {
// Same code to handle the exception
} catch (Exception $e) {
// ...
}
Here you can see that this can become very tedious to do. Looks nothing but this can become a lot of time. So, with this new feature, we can catch many exceptions at once like this:
<?php
try {
// Some code...
} catch (ExceptionType1 | ExceptionType2 $e) {
// Code to handle the exception
} catch (Exception $e) {
// ...
}
Not bad han? ;) This looks cleaner and beautiful.
While we await the release
These are some cool stuff we'll be able to make use of in PHP 7.1. You can probably start learning them now. Note this list is not complete, you can always check the official list of implemented features.
Thanks for reading. If you like it please share it with your friends.