欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP8.3更新內(nèi)容新特性及支持版本探究

 更新時(shí)間:2024年01月12日 08:55:18   作者:沈唁  
每年年底,PHP 項(xiàng)目都會(huì)發(fā)布新的 PHP 主要或次要版本,截至本文發(fā)布時(shí),PHP 8.3 已經(jīng)發(fā)布了?RC6?版本,按照發(fā)布計(jì)劃,正式版將于 11 月 23 日發(fā)布,這篇文章主要為大家介紹了PHP8.3發(fā)布內(nèi)容新特性及支持版本實(shí)例探究

支持版本

除了慶祝新的版本發(fā)布以后,也需要注意一下:PHP 8.0 的生命周期即將結(jié)束,PHP 8.0 早已在2022 年 11 月 26 日結(jié)束了積極支持,而安全支持也將在 PHP8.3 發(fā)布的三天后2023 年 11 月 26 日停止。

了解更多信息可查看Supported Versions

新特性

PHP 8.3 引入了許多新功能。然而,它的功能比 PHP 8.1 或 PHP 8.2 相對(duì)較少。

PHP 8.3 的主要新特性:

  • 類型化類常量
  • 動(dòng)態(tài)類常量獲取
  • #[\Override]屬性
  • 只讀修改
  • 添加json_validate函數(shù)
  • 添加Randomizer::getBytesFromString()方法
  • 添加Randomizer::getFloat()Randomizer::nextFloat()方法

類型化類常量

現(xiàn)在可以在定義常量時(shí),增加類型。

// 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 {
    const PHP = []; // But it may be an array...
}

// 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

動(dòng)態(tài)類常量獲取

在之前的版本中獲取類的常量,除了直接調(diào)用以外,想要?jiǎng)討B(tài)獲取只能通過(guò)拼接后使用constant來(lái)實(shí)現(xiàn),而現(xiàn)在可以直接使用變量來(lái)獲取常量。

這個(gè)方式在枚舉類型中也可以使用。

// 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});

添加#[\Override]屬性

通過(guò)給方法添加 #[\Override] 屬性,PHP 將確保在父類或?qū)崿F(xiàn)的接口中存在同名的方法。

添加該屬性可以清楚地表明重載父類方法是有意為之,并簡(jiǎn)化了重構(gòu)過(guò)程,因?yàn)橹剌d父類方法的刪除會(huì)被檢測(cè)到。

// 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

只讀修改

只讀屬性現(xiàn)在可以在魔術(shù)方法 __clone 方法中修改一次,以實(shí)現(xiàn)只讀屬性的深度克隆。

// PHP < 8.3
readonly class Foo {
    public \DateTime $dateTime;
    function __construct(\DateTime $dateTime) {
        $this->dateTime = $dateTime;
    }
    public function __clone()
    {
        $this->dateTime = clone $this->dateTime;
    }
}
$today = new Foo(new \DateTime());
$tomorrow = clone $today;
// Fatal error: Cannot modify readonly property Foo::$dateTime
// PHP 8.3
readonly class Foo {
    public \DateTime $dateTime;
    function __construct(\DateTime $dateTime) {
        $this->dateTime = $dateTime;
    }
    public function __clone()
    {
        $this->dateTime = clone $this->dateTime;
    }
}
$today = new Foo(new \DateTime());
$tomorrow = clone $today;
$tomorrow->dateTime->modify('+1 day');

添加json_validate函數(shù)

在之前的版本中想要驗(yàn)證一個(gè)字符是否是語(yǔ)法上有效的JSON,需要先decode然后判斷錯(cuò)誤碼,而現(xiàn)在可以直接調(diào)用json_validate函數(shù)。

同時(shí) json_validate() 性能比 json_decode() 要好不少,并且使用更加簡(jiǎn)單。

// 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

一次 Lint 多個(gè)文件

PHP CLI 二進(jìn)制文件的 -l 允許檢查 PHP 文件以確保它沒有語(yǔ)法錯(cuò)誤。

以前只允許一次檢查一個(gè)文件,這意味著如果想檢查整個(gè)項(xiàng)目,則必須為每個(gè)應(yīng)用程序文件調(diào)用一次它。從 PHP 8.3 開始允許傳遞多個(gè)文件。

// PHP < 8.3
php -l index.php
// PHP 8.3
php -l src/**/*.php

除此之外,還有一些新的類、接口和函數(shù),以及棄用和向后兼容性中斷。

具體的內(nèi)容可以期待 PHP 8.3 發(fā)布后查看官方的發(fā)布公告和文檔。

以上就是PHP8.3發(fā)布內(nèi)容新特性及支持版本探究的詳細(xì)內(nèi)容,更多關(guān)于PHP8.3版本特性的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論