
构造函数属性提升 是 php 8 中引入的一项功能,可简化类中的属性声明和初始化。在 php 8 之前,您必须显式声明类属性,然后在构造函数中初始化它们。通过此功能,您可以直接在构造函数的参数列表中声明和初始化属性,从而减少样板代码。
传统语法(php 8 之前)
class product {
private string $name;
private float $price;
public function __construct(string $name, float $price) {
$this->name = $name;
$this->price = $price;
}
}
构造函数属性提升语法 (php 8)
class product {
public function __construct(
private string $name,
private float $price
) {}
}
好处
减少样板代码:
提高可读性:
立即学习“PHP免费学习笔记(深入)”;
支持不变性:
笔记
可见性修饰符:
默认值:
class product {
public function __construct(
private string $name = 'unnamed',
private float $price = 0.0
) {}
}
混合促销和非促销属性:
class product {
private string $category;
public function __construct(
private string $name,
private float $price
) {
$this->category = 'general';
}
}
用例
构造函数属性提升对于像dto(数据传输对象)这样的简单类特别有用,其中主要目的是存储数据。
class customerdto {
public function __construct(
public string $name,
public string $email,
public ?string $phone = null
) {}
}
结论
构造函数属性提升是 php 8 中的一项强大功能,可以提高生产力并降低代码复杂性。对于需要简洁明了初始化的具有多个属性的类来说,它是理想的选择。
在 php 8 中,属性(也称为注解)可以与构造函数属性提升无缝结合,从而产生更干净、更具表现力的代码,尤其是在元数据需要与属性关联的场景中。
与建筑商房产促销整合
通过构造函数属性提升,属性直接在构造函数中定义。您可以使用 attributes 来装饰这些属性并添加上下文信息,而无需单独声明属性。
实际示例
假设您正在处理 dto(数据传输对象)并希望将属性映射到数据库列
没有建筑商房产促销
use attribute;
#[attribute]
class column {
public function __construct(public string $name) {}
}
class user {
#[column('user_id')]
private int $id;
#[column('username')]
private string $name;
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
}
与建筑商房产促销
use attribute;
#[attribute]
class column {
public function __construct(public string $name) {}
}
class user {
public function __construct(
#[column('user_id')] private int $id,
#[column('username')] private string $name
) {}
}
集成的好处
减少样板代码:
更干净、更易读的代码:
反思的灵活性:
通过反射访问属性
// reflection allows us to inspect and manipulate the user class at runtime.
$reflectionclass = new reflectionclass(user::class);
// get the constructor of the user class.
$constructor = $reflectionclass->getconstructor();
// iterate through the constructor's parameters.
foreach ($constructor->getparameters() as $parameter) {
// retrieve all attributes of type column applied to the current parameter.
$attributes = $parameter->getattributes(column::class);
// process each attribute found.
foreach ($attributes as $attribute) {
// instantiate the attribute to access its values.
$column = $attribute->newinstance();
// output the parameter name and the associated column name from the attribute.
echo "parameter: {$parameter->getname()}, column: {$column->name}" . php_eol;
}
}
代码说明
定义列属性:
为构造函数参数添加属性:
使用反射:
访问构造函数:
迭代参数:
获取属性:
实例化属性:
打印元数据:
输出:
Parameter: id, Column: user_id Parameter: name, Column: username
常见用例
数据库映射:
数据验证:
序列化/反序列化:
结论
构造函数属性提升与属性的集成提供了一种在php中构建类的强大而简洁的方法。这在依赖元数据(例如 orm、验证或序列化)的系统中特别有用,使代码更具表现力和组织性。
以上就是PHP ews:构造函数属性提升的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号