New Features in PHP7

This tutorial describes new features and changes in PHP7 and how to use them.
It will be userul for programmers, who want to move from PHP5.x to the new PHP7.x
Provided by Leumas Naypoka

Content


Deprecated

1. The syntax of constructors in the PHP4's style (the name of the constructor method is the same as the class name) is now considered obsolete.

2. Static calls of non-static methods are now considered obsolete.



Improvements

1. Improved performance. PHP 7 runs up to twice as fast as PHP 5.6 or even more.

2. Added support for simplified construction use:

<? use Apphp\Component\{
    
Helper\ClassName,
    
ClassName\SubClassName as MyClass,
    
AnotherClass\AnotherSubClass,
}; 
?>

3. The value of the constants declared with define() can be arrays from now.

4. The INI directive "asp_tags" was deleted. Trying to turn it on will result in a fatal error. ASP tags support (<%) has also was removed.

5. The INI directive "always_populate_raw_post_data" was deleted. The variable $HTTP_RAW_POST_DATA is accordingly no longer available. Instead, use the input stream descriptor php://input.

6. Iterating over an array with foreach() no longer moves the internal array pointer, which can be accessed and changed using the current()/next()/reset() functions and others. Also, foreach by value now always works with a copy of the array.

7. The operator of the left bitwise shift (<<) by the number of bits exceeding the number of bits in integer now always returns 0. Prior to this, the result depended on the architecture of the processor. A similar right shift always gives 0 or -1 depending on the sign of the original number. (Shift does not affect the high-order bit responsible for the sign).

8. Rows containing hexadecimal numbers are now always treated as strings and not treated as numbers: is_numeric("0xFF") is now false, before it was true with all the consequences.

9. The support for the /e modifier in PCRE has been removed. Similar functionality can be implemented by the function preg_replace_callback().

10. Indirect references to variables, properties and methods are now parsed from left to right. You may restore the previous order by curly brackets.

<?
// now parsed as ($$foo)["bar"]["baz"]  - before ${$foo["bar"]["baz"]}
$$foo["bar"]["baz"];
// is parsed as ($foo->$bar)["baz"]    - before $foo->{$bar["baz"]}
$foo->$bar["baz"];
// is parsed as ($foo->$bar)["baz"]()  - before $foo->{$bar["baz"]}()
$foo->$bar["baz"]();
// is parsed as (Foo::$bar)["baz"]()   - before Foo::{$bar["baz"]}()            
Foo::$bar["baz"]();                                                                                      
?>

11. The global keyword takes only simple variables. Instead of global $$foo->bar, you should write global ${$foo->bar}

12. Brackets around variables or function calls no longer affect on behavior. For example, the code where the result of the function is passed by reference:

<?
function getArray() { return [123]; } 
    
$last array_pop(getArray());
    
// Strict Standards: Only variables should be passed by reference
    
$last array_pop((getArray()));
    
// Strict Standards: Only variables should be passed by reference
    // Before it was passed without errors
?>

13. Array elements or object properties that were automatically created during assignments by reference now will have a different order:

<?
$array 
= [];
$array["a"] =& $array["b"];
$array["b"] = 1;
var_dump($array);

Now it will generate ["a" => 1"b" => 1],
Before it was ["b" => 1"a" => 1].            
?>

14. Function list() now assigns variables in direct order (earlier - in reverse), for example:

<?
list($array[], $array[], $array[]) = [123];
var_dump($array);
Not it wil lreturn $array == [123]
Bwfore it was [321]
?>

15. Functionlist() no longer supports unpacking of strings (previously supported in some cases).

<?
$string 
"xy";
list(
$x$y) = $string;
?>
will set $x and $y to null (without warnings) instead of $x == "x" and $y == "y".

16. Functionlist() now it is guaranteed to work with objects that implement the ArrayAccess interface, i.e. Here's how it works:

<?
list($a$b) = (object) new ArrayObject([01]);                                                                                      
?>
Previously, both variables would be null.

17. Iterations in foreach()current()/next()/...

<?
$array 
= [012];
foreach (
$array as &$val) {
    
var_dump(current($array));
}
?>
Now print int(0) three times. Previously, int(1), int(2), bool(false).

18. During iteration of arrays by value, foreach now uses a copy of the array, and its changes within the loop will not affect the behavior of the loop:

<?
$array 
= [012];
$ref =& $array// required to turn on previou sbehauviour
foreach ($array as $val) {
    
var_dump($val);
    unset(
$array[1]);
}
?>
The code prints all the values(0 1 2), before the second element was discarded - (0 2).

19. When the arrays are iterated by reference, changes in the array will affect the loop. It is assumed that PHP will better work out a number of cases, for example, adding to the end of the array:

<?
$array 
= [0];
foreach (
$array as &$val) {
    
var_dump($val);
    
$array[1] = 1;
}
?>
and the added element will also be lost. The output will be "int(0) int(1)", previously there was only "int(0)".

20. The functions func_get_arg() and func_get_args() will now return the current value (not the original value). For example:

<?
function foo($x) {
    
$x++;
    
var_dump(func_get_arg(0));
}
foo(1);
?>
will print "2" instead of "1".

21. In a similar way, exceptions in traces will not output the original values, but already changed:

<?
function foo($x) {
    
$x 42;
    throw new 
Exception;
}
foo("string");
?>
Now will print:
Stack trace: #0 file.php (4): foo (42) # 1 {main}

Previously it would be like this:
Stack trace: #0 file.php (4): foo ('string') # 1 {main}

Although this does not affect the performance, but you should keep this in mind when debugging. The same restriction is now also in debug_backtrace() and other functions that examine arguments.

22. Incorrect octal numbers will produce a compilation error:

<?
$i 
0781// 8 - incorrect digit for octal number
?>
Previously, after an incorrect discharge, it was discarded, and $i would be 7.

23. Bitwise shifts to negative numbers now throw ArithmeticError:

<?
var_dump
(>> -1);
// ArithmeticError: Bit shift by negative number
?>

24. Shift left by a number greater than the number of bits will return 0:

<?
var_dump
(<< 64); // int(0)
?>
Previously, the behavior was depended on the architecture, on x86 and x86-64 the result was == 1, because the shift was cyclic.
Similarly, a shift to the right will give 0 or -1 (depending on the sign):

25. Until PHP7, list() only supported numerically indexed arrays with index starting from zero. We were not able to use it with associative arrays. Now, this feature allows us to specify keys when destructuring arrays.

<?
$user 
= [ "first_name" => "John""last_name" => "Doe"];
[
"first_name" => $firstName"last_name" => $lastName]= $user;
?>
As an interesting side effect, now we can assign only the required keys from the array.
<?
["last_name" => $lastName] = $user;
?>



Additions

1. Added operator "??" (Null coalescing operator), which allows you to check a variable for existence and return its value or the default value. For example, the following construction:

<? $action = isset($_POST['action']) ? $_POST['action'] : 'index'?>

Now can be written as follows:
<? $action $_POST['action'] ?? 'index'?>

2. Added supporting for strings longer than 2 ^ 31 bytes in 64-bit builds.

3. Added syntax \u{xxxxxx} for strings, which allows you to specify any unicode characters in the strings.

4. The value of the constants declared through define() can be arrays now.

<? define("STATUS", array(012)); ?>

5. Added a new comparison operator <=>, also known as "spaceship operator". The $a <=> $b construct returns -1, 0 or +1 if $a is less, equal to or greater than $b. It is convenient to use in callbacks for usort().

6. Reserved keywords can now be used as method names:

<? $object::new("foo""bar")->check(function($k$i) {}); ?>

7. Added neww constant PHP_INT_MIN: the smallest integer supported in this build of PHP. Usually int(-2147483648) in 32 bit systems and int(-9223372036854775808) in 64 bit systems. Usually, PHP_INT_MIN === ~PHP_INT_MAX.

8. Square bracket syntax for list() aka Symmetric array destructuring:

<?
//prior to php 7.1
list($firstName$lastName) = ["John""Doe"];
// php 7.1
[$firstName$lastName] = ["John""Doe"];
?>

9. Class constant visibility (from PHP7.1):

<?
class PostValidator {
    public const 
MAX_SUPER_LENGTH 1000;
    protected const 
MAX_LENGTH 100;
    private const 
MAX_MIN_LENGTH 10;

    public function 
validateTitle($title) {
        if(
strlen($title) > self::MAX_LENGTH) {
            throw new \
Exception("Title is too long");
        }
        return 
true;
    }  
}
?>

10. Void functions. We can specify the type of value a function should return. Sometimes we may want to specify that the function should not return any value.

<?
class Cookie{
    protected 
$jar;

    public function 
set(string $key$value) : void
    
{
        
$this->jar[$key] = $value;
    }
}
?>

11. Catching Multiple Exception Types

<?
try{
  
// somthing
} catch(MissingParameterException $e) {
  throw new \
InvalidArgumentException($e->getMessage());
} catch(
IllegalOptionException $e) {
  throw new \
InvalidArgumentException($e->getMessage());
}
?>

In PHP 7.1, we can handle multiple exceptions in single catch block

<?
try {
  
// something
} catch (MissingParameterException IllegalOptionException $e) {
  throw new \
InvalidArgumentException($e->getMessage());
}
?>

12. Too few arguments exception. As of PHP 7, we can call a function with lesser arguments than what it is actually required. In which case, it will just show a warning about the argument(s) missing, but will continue the function execution. Sometimes this can cause strange or unexpected behavior of the function.

<?
function sayHello($name) {
    echo 
"Hello " $name;
}
sayHello();
// Fatal error: Uncaught ArgumentCountError: Too few arguments to
function sayHello(), 0 passed in...
?>

13. This feature allows us to type hint a function, but still allow null value as parameters or returns. We can make a parameter or return value Nullable by prepending a question mark(?) to the type hint.

<?
function sayHello(?string $name) {
    echo 
"Hello " $name PHP_EOL;
}
sayHello(null); // Hello
sayHello("John"); //Hello John
?>
Similarly, for nullable return type
<?
class Cookie{
    protected 
$jar;
    public function 
get(string $key) : ?string{
        if(isset(
$this->jar[$key])) {
            return 
$this->jar[$key];
        }
        return 
null;
    }
}
?>

Comments


Please post only comments related to the original tutorial. Be polite and helpful, do not spam or offend others.
Create Your Free Account
Please remember that this information is essential to use our services correctly.
After creating the account you will be able to download all of our FREE products.
Fields marked with * are mandatory






Please send me information about updates, new products, specials and discounts from ApPHP!
We recommend that your password should be at least 6 characters long and should be different from your username/email. Please use only letters of the English alphabet to enter your name.

Your e-mail address must be valid. We use e-mail for communication purposes (order notifications, etc). Therefore, it is essential to provide a valid e-mail address to be able to use our services correctly.

All your private data is confidential. We will never sell, exchange or market it in any way. Please refer to Privacy Policy.

By clicking "Create Account", you are indicating that you have read and agree to the ApPHP Terms & Conditions.

Quick Registration with: Facebook / Google