PHP 8.3GA

PHP 8.3 wurde offiziell veröffentlicht . Zu den wichtigsten Änderungen gehören:

  • Typisierte Klassenkonstanten
  • Schreibgeschützte tiefe Kopie des Attributs
  • Fügen Sie neue #[\Override]Attribute hinzu
  • Neue json_validate()Funktion
  • Randomizer::getBytesFromString()Methode hinzufügen
  • Hinzufügen Randomizer::getFloat()und Randomizer::nextFloat()Methode
  • Sowie bessere Leistung, bessere Syntax, verbesserte Typsicherheit

Nachfolgend werden einige Syntaxänderungen vorgestellt.

  • Typisierte Klassenkonstanten

PHP < 8.3

interface I {
    // We may naively assume that the PHP constant is always a string.
    const PHP = 'PHP 8.2';
}

class Foo implements I {
    // But implementing classes may define it as an array.
    const PHP = [];
}

PHP 8.3

interface I {
    const string PHP = 'PHP 8.3';
}

class Foo implements I {
    const string PHP = [];
}

// Fatal error: Cannot use array as value for class constant
// Foo::PHP of type string
  • Klassenkonstanten dynamisch abrufen

PHP < 8.3

class Foo {
    const PHP = 'PHP 8.2';
}

$searchableConstant = 'PHP';

var_dump(constant(Foo::class . "::{$searchableConstant}"));

PHP 8.3

class Foo {
    const PHP = 'PHP 8.3';
}

$searchableConstant = 'PHP';

var_dump(Foo::{$searchableConstant});
  • Schreibgeschützte tiefe Kopie des Attributs

readonlyEigenschaften können jetzt __clonemit einer magischen Methode einmal geändert werden, was tiefe Kopien schreibgeschützter Eigenschaften ermöglicht.

PHP < 8.3

class PHP {
    public string $version = '8.2';
}

readonly class Foo {
    public function __construct(
        public PHP $php
    ) {}

    public function __clone(): void {
        $this->php = clone $this->php;
    }
}

$instance = new Foo(new PHP());
$cloned = clone $instance;

// Fatal error: Cannot modify readonly property Foo::$php

PHP 8.3

class PHP {
    public string $version = '8.2';
}

readonly class Foo {
    public function __construct(
        public PHP $php
    ) {}

    public function __clone(): void {
        $this->php = clone $this->php;
    }
}

$instance = new Foo(new PHP());
$cloned = clone $instance;

$cloned->php->version = '8.3';
  • Fügen Sie neue #[\Override]Attribute hinzu

Durch das Hinzufügen #[\Override]eines Attributs zu einer Methode stellt PHP sicher, dass eine Methode mit demselben Namen in der übergeordneten Klasse oder der implementierten Schnittstelle vorhanden ist.
Das Hinzufügen dieses Attributs macht deutlich, dass das Überschreiben der übergeordneten Methode beabsichtigt ist, und vereinfacht den Refactoring-Prozess, da das Löschen der überschriebenen übergeordneten Methode erkannt wird.

PHP < 8.3

use PHPUnit\Framework\TestCase;

final class MyTest extends TestCase {
    protected $logFile;

    protected function setUp(): void {
        $this->logFile = fopen('/tmp/logfile', 'w');
    }

    protected function taerDown(): void {
        fclose($this->logFile);
        unlink('/tmp/logfile');
    }
}

// The log file will never be removed, because the
// method name was mistyped (taerDown vs tearDown).

PHP 8.3

use PHPUnit\Framework\TestCase;

final class MyTest extends TestCase {
    protected $logFile;

    protected function setUp(): void {
        $this->logFile = fopen('/tmp/logfile', 'w');
    }

    #[\Override]
    protected function taerDown(): void {
        fclose($this->logFile);
        unlink('/tmp/logfile');
    }
}

// Fatal error: MyTest::taerDown() has #[\Override] attribute,
// but no matching parent method exists
  • Neue json_validate()Funktion

json_validate()json_decode()Sie können mit JSON effizienter prüfen, ob eine Zeichenfolge syntaktisch korrekt ist .

PHP < 8.3

function json_validate(string $string): bool {
    json_decode($string);

    return json_last_error() === JSON_ERROR_NONE;
}

var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

PHP 8.3

var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

Darüber hinaus erreicht PHP 8.0 das Ende seiner Lebensdauer. Bereits am 26. November 2022 endete der aktive Support für PHP 8.0 und auch der Sicherheitssupport endet am 26. November 2023, drei Tage nach der offiziellen Veröffentlichung von PHP 8.3.

Supongo que te gusta

Origin www.oschina.net/news/267734/php-8-3-ga
Recomendado
Clasificación