PHP 8.4 Released - property hooks, asymmetric visibility, an updated DOM API

A Year of Hard Work and Collaboration

A total of 115 contributors, have made over 2,600 commits since PHP 8.3 until PHP 8.4.0 release.
PHP 8.4 includes changes from 36 RFCs.
Since PHP 8.0, PHP 8.4 received the highest number of RFCs, and PHP 8.4 brings the changes such as property hooks and asymmetric visibility that received significant community involvement and refinement.
PHP Core Roundup #20: PHP 8.4 is Released! — The PHP Foundation — Supporting, Advancing, and Developing the PHP Language

New ext-dom features and HTML5 support

$dom = Dom\HTMLDocument::createFromString(
    <<<HTML
        <main>
            <article>PHP 8.4 is a feature-rich release!</article>
            <article class="featured">PHP 8.4 adds new DOM classes that are spec-compliant, keeping the old ones for compatibility.</article>
        </main>
    HTML,
    LIBXML_NOERROR,
);
$node = $dom->querySelector('main > article:last-child');
var_dump($node->classList->contains("featured")); // bool(true)

Property hooks

class Locale
{
    public string $languageCode;
    public string $countryCode {
        set (string $countryCode) {
            $this->countryCode = strtoupper($countryCode);
        }
    }
    public string $combinedCode {
        get => \sprintf("%s_%s", $this->languageCode, $this->countryCode);
        set (string $value) {
            [$this->languageCode, $this->countryCode] = explode('_', $value, 2);
        }
    }
}

Asymmetric Visibility

class PhpVersion
{
    public private(set) string $version = '8.4';
    // un fel de public readonly dar poate fii modificat intern
}

#[\Deprecated] Attribute

class PhpVersion
{
    #[\Deprecated(
        message: "use PhpVersion::getVersion() instead",
        since: "8.4",
    )]
    public function getPhpVersion(): string {
        return '8.4';
    }
}
$phpVersion = new PhpVersion();
// Deprecated: Method PhpVersion::getPhpVersion() is deprecated since 8.4, use PhpVersion::getVersion() instead
echo $phpVersion->getPhpVersion();

New array_*() functions

New functions array_find(), array_find_key(), array_any(), and array_all() are available.

$animal = array_find(
    ['dog', 'cat', 'cow', 'duck', 'goose'],
    static fn (string $value): bool => str_starts_with($value, 'c'),
);
var_dump($animal); // string(3) "cat"

Object API for BCMath

use BcMath\Number;
$num1 = new Number('0.12345');
$num2 = new Number('2');
$result = $num1 + $num2;
echo $result; // '2.12345'

PDO driver specific subclasses

New subclasses Pdo\Dblib, Pdo\Firebird, Pdo\MySql, Pdo\Odbc, Pdo\Pgsql, and Pdo\Sqlite of PDO are available.

new MyClass()->method() without parentheses

class PhpVersion {
    public function getVersion(): string {
        return 'PHP 8.4';
    }
}
var_dump(new PhpVersion()->getVersion());

Personal noul Dom\HTMLDocument mi se pare foarte misto acum ca se poate face fara librarii externe, noile array functions sunt utile, si property hooks sunt misto.

3 Likes

HTMLDoc, property hooks si vizibilitatea asimetrica sunt misto

1 Like