Tuesday 19 January 2016

PHP 7 new features

PHP 7 new features

Following are PHP 7 new features

  1. Scalar type declarations: In this we can declare what type of argument will be accepted by functions.
    function sumOfInts(int ...$all_ints){ /** It will accept only integers **/
        return array_sum($all_ints);
    }
    echo sumOfInts(2, 3); //5
    echo sumOfInts(2, '3', '5'); //10
    echo sumOfInts(2, '3', 'string'); //Error: Uncaught TypeError: Argument 3 passed to sumOfInts() must be of the type integer, string given
    
  2. Return type declarations : In this we can declare what what will be the datatype from functions.
    function sumOfInts(int ...$all_ints):int
    {
        return array_sum($all_ints);
    }
    echo sumOfInts(2, 3); //5
    echo sumOfInts(2, '3', '5'); //10
    
    Here if function "sumOfInts" return an string OR Array, it will give error.
  3. Null coalescing operator (??) have been added.
    $userId = $_GET['user_id'] ?? '0';
    is equivalent to
    $userId = isset($_GET['user_id']) ? $_GET['user_id'] : '0';
  4. Spaceship operator :The spaceship operator is used for comparing two expressions. It returns -1, 0 or 1
    echo 1 <=> 1; // 0
    echo 1 <=> 2; // -1
    echo 2 <=> 1; // 1
    
  5. define() updates: Now you can add array.
    define('CLIENTS', [
        'client 1',
        'client 2',
        'client 3'
    ]);
    
    echo CLIENTS[1];//client 2
    
  6. Unicode codepoint escape syntax
    echo "\u{aa}";
    echo "\u{9999}";
  7. Closure::call Temporarily binding an object scope to a closure and invoking it.
    class A {private $x = 1;}
    $getX = function() {return $this->x;};
    echo $getX->call(new A);
  8. unserialize updates: Provide better security when unserializing objects on untrusted data and prevents possible code injections by enabling the developer to whitelist classes that can be unserialized.
    $data = unserialize($searilizedData, ["allowed_classes" => ["MyClass", "MyClass2"]]);
  9. list() function updates: Now list() can unpack the object also. Earlier it unpack int, float, string and array only.
  10. session_start() function updates:
    Now you can pass array-options in this function. For Example:
    session_start([
        'cache_limiter' => 'private',
        'read_and_close' => true,
    ]);
  11. intdiv() new function It performs an integer division of its operands and returns it. For Example:
    echo intdiv(100, 3); //33
  12. use updations Classes, functions and constants being imported from the same namespace can now be grouped together in a single use statement Below both are Same.
    use some\namespace\ClassA;
    use some\namespace\ClassB;
    use some\namespace\ClassC as C;
    OR
    use some\namespace\{ClassA, ClassB, ClassC as C};
  13. CSPRNG Functions i.e. random_bytes() and random_int()
    $bytes = random_bytes(5);
    var_dump(bin2hex($bytes)); //string(10) "385e33f741"
    var_dump(random_int(1, 100));//1-100
  14. Generator delegation: Generators can now delegate to another generator using yield, Traversable object/array automatically.
    function func1()
    {
        yield 1;
        yield 2;
        yield from func2();
    }
    
    function func2()
    {
        yield 3;
        yield 4;
    }
    
    foreach (func1() as $val)
    {
        echo $val, PHP_EOL;
    }
    /** Output 
    1
    2
    3
    4
    Output **/