四时宝库

程序员的知识宝库

PHP 8.4:新特性与改进,PHP界的又一次飞跃

PHP,这个全球最流行的服务器端脚本语言,刚刚迎来了它的最新版本——PHP 8.4。这个版本不仅带来了一系列令人兴奋的新特性,还对现有功能进行了改进和优化。在这篇文章中,我们将深入探讨PHP 8.4的新特性,以及它们如何影响开发者的工作和PHP的未来。

属性钩子(Property hooks)RFC文档

在PHP 8.4之前,开发者需要通过getter和setter方法来处理类的属性。这种方式虽然有效,但有时显得冗余且难以维护。PHP 8.4引入了属性钩子,允许开发者直接在属性定义中指定getter和setter,从而简化了代码并提高了IDE和静态分析工具的兼容性。

PHP 8.4之前

class Locale {
    private string $languageCode;
    private string $countryCode;
    
    public function __construct(string $languageCode, string $countryCode) {
        $this->setLanguageCode($languageCode);
        $this->setCountryCode($countryCode);
    }
    
    public function setCountryCode(string $countryCode): void {
        $this->countryCode = strtoupper($countryCode);
    }
    
    public function getCombinedCode(): string {
        return sprintf("%s_%s", $this->languageCode, $this->countryCode);
    }
}

PHP 8.4之后

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) {
            list($this->countryCode, $this->languageCode) = explode('_', $value, 2);
        }
    }
}

不对称可见性(Asymmetric Visibility)RFC文档

PHP 8.4引入了不对称可见性属性,允许开发者独立控制属性的读写权限。这意味着你可以让一个属性只读或只写,而不需要额外的getter方法。

PHP 8.4之前

class PhpVersion {
    private string $version = '8.3';
    
    public function getVersion(): string {
        return $this->version;
    }
    
    public function increment(): void {
        [$major, $minor] = explode('.', $this->version);
        $minor++;
        $this->version = "{$major}.{$minor}";
    }
}

PHP 8.4之后

class PhpVersion {
    public private(set) string $version = '8.4';
    
    public function increment(): void {
        [$major, $minor] = explode('.', $this->version);
        $minor++;
        $this->version = "{$major}.{$minor}";
    }
}

#[\Deprecated]属性

PHP 8.4引入了新的#[\Deprecated]属性,使得PHP的废弃机制可以应用于用户定义的函数、方法和类常量。

PHP 8.4之前

class PhpVersion {
    /**
     * @deprecated 8.3 use PhpVersion::getVersion() instead
     */
    public function getPhpVersion(): string {
        return $this->getVersion();
    }
    
    public function getVersion(): string {
        return '8.3';
    }
}

PHP 8.4之后

class PhpVersion {
    #[\Deprecated(
        message: "use PhpVersion::getVersion() instead",
        since: "8.4",
    )]
    public function getPhpVersion(): string {
        return $this->getVersion();
    }
    
    public function getVersion(): string {
        return '8.4';
    }
}

新的ext-dom特性和HTML5支持

PHP 8.4引入了新的DOM API,包括对HTML5文档的标准兼容支持,修复了DOM功能的长期兼容性问题,并添加了多个函数以方便文档操作。

PHP 8.4之前

$dom = new DOMDocument();
$dom->loadHTML(
    <<<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,
);

PHP 8.4之后

$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,
);

对象API for BCMath

PHP 8.4引入了BcMath\Number对象,使得在处理任意精度数字时可以使用面向对象的方式和标准数学运算符。

PHP 8.4之前

$num1 = '0.12345';
$num2 = 2;
$result = bcadd($num1, $num2, 5);
echo $result; // '2.12345'

PHP 8.4之后

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

新的array_*()函数

PHP 8.4引入了新的array_*()函数,使得数组操作更加方便。

PHP 8.4之前

$animal = null;
foreach (['dog', 'cat', 'cow', 'duck', 'goose'] as $value) {
    if (str_starts_with($value, 'c')) {
        $animal = $value;
        break;
    }
}
var_dump($animal); // string(3) "cat"

PHP 8.4之后

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

PDO驱动特定子类

PHP 8.4引入了PDO的特定驱动子类,使得数据库操作更加灵活和强大。

PHP 8.4之前

$connection = new PDO(
    'sqlite:foo.db',
    $username,
    $password,
);

PHP 8.4之后

$connection = PDO::connect(
    'sqlite:foo.db',
    $username,
    $password,
);

无需括号的新实例化语法

PHP 8.4允许在实例化对象后直接访问属性和方法,无需将new表达式放在括号中。

PHP 8.4之前

var_dump((new PhpVersion())->getVersion());

PHP 8.4之后

var_dump(new PhpVersion()->getVersion());

新类、接口和函数

PHP 8.4还引入了许多新的类、接口和函数,包括懒加载对象、新的JIT实现、新的request_parse_body()函数等。

PHP 8.4也带来了一些废弃和向后兼容性中断的变更,包括IMAP、OCI8、PDO_OCI和pspell扩展被移动到PECL,隐式可空参数类型被废弃等。

PHP 8.4的发布,不仅是PHP语言的一次重大更新,也是对开发者工作流程的一次优化。新特性的加入,使得PHP在性能、安全性和开发效率上都有了显著提升。随着PHP 8.4的普及,我们有理由相信,PHP将继续在服务器端脚本语言领域保持其领先地位。

科技脉搏,每日跳动。

——敖行客Allthinker与您共享未来之声

- 智慧链接 思想协作 -

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接