PHP 7.0 New Features

in #php6 years ago

php7image1.png

1. Scalar Type Hints

New in PHP 7 is the ability to type hint scalar types. Scalar types are the most basic primitive types like integers, floats, booleans, or strings. Before PHP 7 we were only able to type hint more complex types like arrays and classes. This code will work in PHP 5.6 as well.

<?php

function doSomething( array $things ) {
    var_dump( $things );
}

If the above function is called without an array as its argument it will throw a fatal error. But now with PHP 7 we could type hint scalar types as well.

<?php

function makeDouble( int $value ) {
    return $value * 2;
}

Let's see it in action. If I would call this function with string as its argument like this

makeDouble( 'double this text' );

A fatal error with uncaught type exception will be thrown. Now if I would call this function with an integer.

makeDouble( 4 );

It will return 16. Also, type hints are not entirely strict. If I would call this function with 4 as a string, PHP will do its best to translate that variable to whatever you declared. So this

makeDouble( '4' );

will work as well. Also if you want to be as strict as possible so that php would not translate you could declare strict_types=1. Now this code will blow up.

<?php

declare( strict_types=1 );

function makeDouble( int $value ) {
    return $value * 2;
}

makeDouble( '4' );

2. Return Type Declarations

We can now type hint return values in PHP 7 as well. For example, if we have a doPayment function and we expect the Payment instance to be returned. Code will look something like this.

<?php

class Payment{}

function doPayment(){
    return new Payment;
}

var_dump(doPayment());

This code will return a Payment object. However, we could return an integer as well. There is no protection. If you want things to be strict you could add a return type declaration. You need to add a semicolon after the parenthesis followed by the return type.

<?php

class Payment{}

function doPayment() : Payment{
    return new Payment;
}

var_dump(doPayment());

Now if you do not return a Payment instance, a fatal error with uncaught type exception will be thrown. One important thing is that it will only be detected at runtime.

<?php

class Payment{}

function doPayment() : Payment{
    return [];
}

Yes, the above code is incorrect but because I have not called this method, it will not return an error.

But if we do call that function, it will blow up. Like this

<?php

class Payment{}

function doPayment() : Payment{
    return [];
}

var_dump(doPayment());

3. Spaceship operator

Spaceship operator also called as combined comparison operator has the following symbol <=>
Take a look at this

<?php

$a=1;
$b=2;
echo $b <=> $a;
echo $a <=> $b;
echo $a <=> $a;

Spaceship operator returns -1, 0, 1. The formula for that is

  • if $a < $b it will return -1
  • if $a = $b it will return 0
  • and if $a > $b it will return 1
    So the above code will echo 1-10

4. Null Coalesce Operator

New in PHP 7 is the null coalesce operator and this how it looks like ??
Remember when we were used to do some stuff like this where we grab something from the GET super global. But that may not exist, so we end up doing some annoying stuff like this

<?php
$search = isset( $_GET['search'] ) ? $_GET['name'] : '';

Now we could rewrite that using the null coalesce operator like this

$search = $_GET['search'] ?? '';

So if we have $_GET['search'] we will set that other wise we will set $search to an empty string.

5. Grouped Imports

When importing with the use keyword, we can now group declarations. Let's say we have a PHP file where we are importing some classes from the same namespace.

<?php

use App\Person;
use App\Mail;
use App\Admin;
use App\Home\Main;

We could replace the above code to something like this

<?php

use App\{Person, Mail, Admin, Home\Main};

We could go as far as we want inside the App namespace. We could import another class that is in App\Foo\Bar\Main\Home to the above code. Now the import statement will look like this

<?php

use App\{Person, Mail, Admin, Home\Main, Foo\Bar\Main\Home};

6. Anonymous Classes

In PHP we could create anonymous functions and we call them closures. But now in PHP 7, we could also create anonymous classes. Let's say we new up some class Payment. We call setPayment method that accepts a class. We could send an anonymous class to that method.

<?php

interface PaymentMethod {
    public function method();
}

class Payment {
    public function setPayment( PaymentMethod $payment_method ) {
        var_dump( $payment_method->method() );
    }
}

class Stripe implements PaymentMethod {
    public function method() {
        return 'stripe payment method';
    }
}

$payment=new Payment();
$payment->setPayment(new Stripe());

We have a Payment class that accepts a concrete implementation of the PaymentMethod interface. After that, we create a Stripe class and then pass it on to the setPayment method. Instead of that Stripe we could use anonymous class. Like this

<?php

interface PaymentMethod {
    public function method();
}

class Payment {
    public function setPayment( PaymentMethod $payment_method ) {
        var_dump( $payment_method->method() );
    }
}

$payment=new Payment();
$payment->setPayment(new class implements PaymentMethod {
    public function method() {
        return 'stripe payment method';
    }

});

Both of them works exactly the same way. In anonymous classes we could also use inherit and implements keywords the same way we use them when defining classes.

7. Constant arrays using define()

Array constants can now be defined using the define().

<?php
define('GAMES', [
    'Call of Duty',
    'Battlefield',
    'Assassins Creed',
    'Need for Speed'
]);

There are many new features in PHP 7, I only discussed some of the most important one. For more information read the official documentation here

PHP 7.0 new features

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.034
BTC 66258.39
ETH 3170.93
USDT 1.00
SBD 4.07