Breaking

Followers

Monday, 13 June 2016

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 some 8 cool features that have already been implemented in PHP 7.1. Many RFCs are still under vote. Probably many is still to come. In the other hand they 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.


1. Nullable Types

In the first release of PHP 7 the return type declaration was made possible. Which mean 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
}


2. Square Bracket Syntax For Array Destructuring Assignment

I there is not need to talk much about this one since I've dedicated an entire article for it some days ago. 

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, error like 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

4. Allow Specifying Keys In List()

This work with the array destructuring. It will allows us to specify key in the arrays while desstructuring them with list() function which wasn'tt possible before.


5. Generalize Support Of Negative String Offsets

This is a feature which was voted by unanimity. It's simply useful. If you've faced the situation where by 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 occurence from the end of the character. If it's a positive number it strats from thebeginning:
<?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 occurence of needle in a string. It's 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];


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, beside 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 voidtype 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 key word like intfloat, 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
}


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 dont 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 publicprivate, or protected. Now from PHP 7.1, we can do it. That's what I am talking about :-)


8. Catching Multiple Exception Types

This is a tiny feature but which will save a lot of headache 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 ver tedious to do. Looks nothing but this can become a lot in time. So, with the 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.

No comments:

Post a Comment