PHP 7.1

(Dezember 2016)

Alle Änderungen im Überblick

Alexander Bogomolov / @abogomolov

Versionen im Überblick

Currently Supported Versions Quelle: Supported Versions
Currently Supported Versions Quelle: Supported Versions

Geschwindigkeit

ca. 7% schneller

Nicht kompatible Änderungen (Quelle)

  • Fatal error bei zu wenig Argumenten (früher Warnung)
    function test($param){}
    test();
  • Keine Klasse, Interfaces oder Traits mit den Namen "void" oder "iterable"
  • mt_rand() nutzt die neue Version des Mersenne Twister Algorithmuses
  • rand() und srand() sind nun Aliase für mt_rand() and mt_srand()
  • Keine dynamischen Aufrufe folgender Funktionen: assert() (with a string as the first argument),
    compact(),
    extract(),
    func_get_args(),
    func_get_arg(),
    func_num_args(),
    get_defined_vars(),
    mb_parse_str() (with one arg),
    parse_str() (with one arg)
    (function () {
        $func = 'func_num_args';
        $func();
    })();
  • allowed_classes aus den unserialize()-Optionen ist strikt typisiert
  • DateTime und DateTimeImmutable beinhalten Mikrosekunden
    new DateTime() == new DateTime(); // false
  • Zahlreiche Fatal errors wurden zu Error exceptions umgewandelt
  • Variablen die per use in ein closure übergeben werden dürfen keine Namen von Superglobalen haben
    $f = function () use ($_SERVER) {};
    $f = function () use ($this) {};
    $f = function ($param) use ($param) {};

Neue Features (Quelle)

Nullable types

function testReturn(): ?string {
    return 'elePHPant';
}

function testReturn(): ?string {
    return null;
}

function test(?string $name) {
    var_dump($name);
}

Void functions

function voidTest($a, $b): void {
    $this->sum = $a + $b;
}

function voidTest($a, $b): void {
    return;
}

Array "Destrukturierung"

$data = [
    [1, 'Tom'],
    [2, 'Fred'],
];

// list() style
list($id1, $name1) = $data[0];

// [] style
[$id1, $name1] = $data[0];

// list() style
foreach ($data as list($id, $name)) {}

// [] style
foreach ($data as [$id, $name]) {}

Sichtbarkeit von Konstanten

class ConstDemo
{
    const PUBLIC_CONST_A = 1;
    public const PUBLIC_CONST_B = 2;
    protected const PROTECTED_CONST = 3;
    private const PRIVATE_CONST = 4;
}

iterable pseudo-type

function iterator(iterable $iter) {
    foreach ($iter as $val) {}
}

Multi catch exception handling

try {
    // some code
} catch (FirstException | SecondException $e) {
    // handle first and second exceptions
}

Keys in list()

$data = [
    ["id" => 1, "name" => 'Tom'],
    ["id" => 2, "name" => 'Fred'],
];

// list() style
list("id" => $id1, "name" => $name1) = $data[0];

// [] style
["id" => $id1, "name" => $name1] = $data[0];

Negative string offsets

var_dump("abcdef"[-2]); // string (1) "e"

Callables zu Closures umwandeln

class Test {
    public function exposeFunction() {
        return Closure::fromCallable([$this, 'privateFunction']);
    }

    private function privateFunction($param) {
        var_dump($param);
    }
}

$privFunc = (new Test)->exposeFunction();
$privFunc('some value');

Neue Funktionen (Quelle)

  • is_iterable()
  • Closure::fromCallable()
  • session_create_id()
  • session_gc()
  • und andere

Geänderte Funktionen (Quelle)

  • getopt()
  • getenv()
  • get_headers()
  • long2ip()
  • output_reset_rewrite_vars()
  • parse_url()
  • unpack()
  • json_encode()
  • mb_ereg() und mb_ereg_replace()
  • PostgreSQL

Veraltet (Quelle)

  • ext/mcrypt
  • e-Modifikator von mb_ereg_replace() und mb_eregi_replace()

Fragen?

Vielen Dank!