Logo
Section 1 / 10

使用构造函数属性提升

学习如何通过构造函数属性提升显著减少代码行数。

导入

使用构造函数属性提升

学习如何通过构造函数属性提升显著减少代码行数。

1<?php 2declare(strict_types=1); 3 4// 声明带有构造函数属性提升的 Test 类 5class Test 6{ 7 public function __construct( 8 public int $id, 9 public int $token = 0, 10 public string $name = '' 11 ) { } 12} 13 14// 创建 Test 类的实例 15$test = new Test(999); 16echo '<pre>'; 17var_dump($test); 18echo '</pre>'; 19?>
1text = $text; 2 $this->fontFile = $fontFile; 3 $this->width = $width; 4 $this->height = $height; 5 $this->size = $size; 6 $this->angle = $angle; 7 $this->textX = $textX; 8 $this->textY = $textY; 9 // 其他代码未显示 10 } 11} 12?>

第一部分:PHP 8 技巧

使用属性