<?php
// PHP 8.2 示例代码
// 示例 1: 使用只读类 (Read-Only Classes)
class Point {
public function __construct(
public readonly int $x,
public readonly int $y,
) {}
}
$point = new Point(1, 2);
echo $point->x; // 输出: 1
// $point->x = 3; // 这会抛出错误,因为属性是只读的
// 示例 2: 使用枚举 (Enums)
enum Status: string {
case Active = 'active';
case Inactive = 'inactive';
}
echo Status::Active->value; // 输出: active
// 示例 3: 使用新的 array_is_list 函数
$array = [1, 2, 3];
var_dump(array_is_list($array)); // bool(true)
$assocArray = ['a' => 1, 'b' => 2];
var_dump(array_is_list($assocArray)); // bool(false)
// 示例 4: 使用新的 str_starts_with 和 str_ends_with 函数
$string = "Hello, World!";
var_dump(str_starts_with($string, "Hello")); // bool(true)
var_dump(str_ends_with($string, "!")); // bool(true)
?>
以上代码展示了 PHP 8.2 中的一些新特性,包括只读类、枚举、array_is_list 函数以及 str_starts_with 和 str_ends_with 函数。
上一篇:php计算两个日期之间的天数
下一篇:php 图片压缩
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站