PHP 8中类属性默认值必须为常量表达式:标量、数组字面量、null/true/false、类/全局常量;禁止运行时计算如函数调用、new对象、超全局变量;readonly属性(PHP 8.3+)同理,且实例化后不可修改。

PHP 8 中自定义类属性的默认值写法,核心原则是:**必须为常量表达式,不能含运行时计算逻辑**。无论是普通属性还是只读(readonly)属性,都遵循这一底层限制。
PHP 7.4+ 支持在声明属性时直接赋值,但仅限以下类型:
public string $status = 'pending';
protected bool $active = true;、private int $retry = 3;
public array $roles = ['user', 'guest'];
public ?string $note = null;
public string $env = APP_ENV; 或 public string $mode = self::MODE_DEV;
以下写法会报错:public string $now = date('Y-m-d');、public Logger $logger = new Logger();、public string $path = $_SERVER['DOCUMENT_ROOT'];——这些都属于运行时表达式,PHP 编译期无法解析。
PHP 8.3 新增能力:readonly 属性也可带默认值,语义更清晰,适用 DTO、配置类等场景。
public readonly string $format = 'json';
public readonly array $headers = ['Content-Type' => 'application/json'];
public readonly int $timeout = 30;,若构造函数中写了 $this->timeout = $custom;,则覆盖默认值;否则保留 30注意:一旦对象实例化完成,该属性值不可再修改,否则抛出 Fatal error: Cannot modify readonly property。
PHP 8 开始广泛支持属性类型声明,搭配默认值需注意兼容性:
public string $name = 'anon'; ✅,但 public string $name = null; ❌(null 不属于 string)public ?string $name = null; ✅public string|int $id = 'default'; ✅public array $data = []; ✅,public array $data = null; ❌(除非声明为 ?array)常见误用,编译阶段即报错:
public string $token = generateToken(); —— 函数调用不被允许public array $config = parse_ini_file('app.ini'); —— 运行时 I/O 操作public string $host = getenv('DB_HOST'); —— 超全局变量非编译时常量public User $user = new User(); —— 对象实例化(PHP 8.1+ 允许在函数参数中 new,但类属性仍禁止)动态初始化请移至 __construct() 中处理:$this->user = new User();。
