Things to remember why PHP 7 is great.

In PHP 7.0:

New features:
https://www.php.net/manual/en/migration70.new-features.php
Depreciated features
https://www.php.net/manual/en/migration70.deprecated.php

PHP Sandbox online here

Catch errors!

We can catch errors or exceptions as well as both using Throwable interface!

<?php
try {
    undefinedFunction();
} catch (Throwable $error) {
    echo 'Now if you write bad code, you can catch it! ' . $error->getMessage();
}
echo "nnContinue processing file...";

Scalar Type Hints

Scalar Type Hints and strict declaration. By default, type hinting will force the argument value into the type of type hint.

Strict Mode

Strict mode need to be declared for each file. If it is not set, it will default to weak mode. When ever you use strict mode in a file. You need to declare strict mode on the very top of the file.

<?php
declare(strict_types = 1);

...

It is possible to enable strict mode on a per-file basis. In strict mode, only a variable of exact type of the type declaration will be accepted, or a TypeError will be thrown. The only exception to this rule is that an integer may be given to a function expecting a float.

http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict

It’s allowed as a widening primitive conversion:

A widening primitive conversion does not lose information about the overall magnitude of a numeric value.

Return types hinting

Added in PHP 7.0

Type hinting was also added to return types. You can use it as in the example below:

declare(strict_types = 1); // no casting
    
function getName(): string // only string allowed
    {
         return "My Name";
    }

Updated In PHP 7.1

Nullable type hint was added!

declare(strict_types = 1); // no casting
 
function getName(): ?string // null or string allowed
    {
         return null;
    }

Void as return type hint (from PHP 7.1)

If you define void as return type, the function will expect you to return nothing or use the return statement without any value. If you pass null as return value, your code will throw a Internal Server Error 500, a FatalErrorException.

Class constant can be set to private!

If you want to force on using method over direct access of a class constant, you can set it scope to be private. Then create a method that will return this value when needed.

Iterable interface (from PHP 7.1)

A new pseudo-type (similar to callable) called iterable has been introduced. It may be used in parameter and return types, where it accepts either arrays or objects that implement the Traversable interface. With respect to subtyping, parameter types of child classes may broaden a parent’s declaration of array or Traversable to iterable. With return types, child classes may narrow a parent’s return type of iterable to array or an object that implements Traversable.

Multi Exception Catch

Use Spaceship operator <=>

echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

Use Null coalescing operator $x ?? "some value"

<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
echo "${username}";

Enjoy overall speed improvement

Use rand() as an alias to mt_rand()

Which provides a better random value.

We can now use group use declarations

// // from PHP 7.0
use somenamespace{ClassA, ClassB, ClassC as C};
use function somenamespace{fn_a, fn_b, fn_c};
use const somenamespace{ConstA, ConstB, ConstC};

Function carrying like similar to JS – Example

<?php // from PHP 7.0

// A curried function
function add($a) {
  return function($b) use ($a) {
    return $a + $b;
  };
}

// Invoking curried function in PHP 7
$result = add(10)(15);

var_dump($result); // int 25

PHP CHANGELOG:

https://www.php.net/manual/en/appendices.php

0
Would love your thoughts, please comment.x
()
x