pense-bête de bruno sanchiz

Accueil > Programmation > PHP > php:nouvelles fonctionnalités

php:nouvelles fonctionnalités

Publié le 8 avril 2017, dernière mise-à-jour le 26 août 2019, 5 visites, visites totales.

https://secure.php.net/manual/en/appendices.php
https://secure.php.net/manual/en/migration71.new-features.php

strict_types=1

declare(strict_types=1);
function test(int $p){ echo $p;}
test('1'); // ne marche pas 

type du retour de fonction

function test():array{ return[0;1;2];} // valide
function test():array{ return 0;} // invalide

Null coalescing operator

$username = $_GET['user'] ?? 'nobody';
/équivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

Spaceship operator

echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1

Unicode codepoint escape syntax

echo "\u{9999}";

namespace

use some\namespace\ClassB;
use some\namespace\ClassC as C;

résumé en
use some\namespace\{ClassA, ClassB, ClassC as C};

itération

function gen()
{
    yield 1;
    yield 2;
    yield from gen2();
}
function gen2()
{
    yield 3;
    yield 4;
}
foreach (gen() as $val)
{
    echo $val, PHP_EOL;
}

division entière

intdiv()

session_start

session_start([
    'cache_limiter' => 'private',
    'read_and_close' => true,
]);

PHP 5.6
opérateur ...

function f($req, $opt = null, ...$params) {
    // $params is an array containing the remaining arguments.
    printf('$req: %d; $opt: %d; number of params: %d'."\n",
           $req, $opt, count($params));
}

opérateur ...

function add($a, $b, $c) {
    return $a + $b + $c;
}
$operators = [2, 3];
echo add(1, ...$operators);

__debugInfo()

https://secure.php.net/manual/en/migration56.new-features.php

The __debugInfo() magic method has been added to allow objects to change the properties and values that are shown when the object is output using var_dump().

[bruno sanchiz]