สำรวจกลไกภายในของแอตทริบิวต์, readonly, first-class callable และการกำหนดชนิดแบบเข้มงวดของ PHP 8
เปิดบทเรียนนี้ใน Kodokonแอตทริบิวต์ (attribute) แทนที่คำอธิบายประกอบแบบ docblock ด้วยคลาสจริง ที่ตรวจสอบความถูกต้องโดยเอนจิน แอตทริบิวต์เป็นเพียงเมทาดาทาที่หลับใหลอยู่ การเขียน #[Route('/users')] ไม่ได้สร้าง อ็อบเจกต์ใด ๆ มันจะมีชีวิตขึ้นมาก็ต่อเมื่อโค้ดบางส่วนสอบถามมันผ่านรีเฟลกชัน (reflection) และเรียก newInstance() ซึ่งเป็นสิ่งที่เฟรมเวิร์กทำตอนเริ่มต้นระบบเป๊ะ ๆ เพื่อสร้างตารางเส้นทางของมัน เป้าหมายที่อนุญาต (TARGET_METHOD, TARGET_CLASS...) ก็ถูกประกาศโดยแอตทริบิวต์บนคลาสแอตทริบิวต์เองเช่นกัน
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_METHOD)]
final class Route
{
public function __construct(
public readonly string $path,
public readonly string $method = 'GET'
) {}
}
final class UserController
{
#[Route('/users', method: 'GET')]
public function index(): array
{
return ['users' => []];
}
}
$ref = new ReflectionMethod(
UserController::class,
'index'
);
foreach ($ref->getAttributes(Route::class) as $attr) {
$route = $attr->newInstance();
echo $route->method, ' ', $route->path, PHP_EOL;
}readonly ซึ่งใช้กับคุณสมบัติได้ใน 8.1 และใช้กับทั้งคลาสได้ใน 8.2 รับประกันว่าคุณสมบัติจะถูกเขียนเพียง ครั้งเดียว จากภายในขอบเขตของคลาสเท่านั้น มันคือเครื่องมือสำหรับ value object การ "แก้ไข" ใด ๆ จะกลายเป็นการสร้างอ็อบเจกต์ใหม่ ซึ่งกำจัดบั๊กทั้งตระกูลที่เกิดจากสถานะที่ใช้ร่วมกัน (shared state) คลาส readonly กำหนดให้คุณสมบัติทั้งหมดของมันต้องมีการระบุชนิดและต้องไม่เป็นแบบสแตติก
<?php
declare(strict_types=1);
final readonly class Money
{
public function __construct(
public int $amount,
public string $currency
) {}
public function add(Money $other): self
{
if ($other->currency !== $this->currency) {
throw new InvalidArgumentException(
'Currency mismatch'
);
}
return new self(
$this->amount + $other->amount,
$this->currency
);
}
}
$total = new Money(500, 'EUR');
$total = $total->add(new Money(250, 'EUR'));
echo $total->amount;ไวยากรณ์ first-class callable strtoupper(...) (PHP 8.1) สร้าง Closure จากฟังก์ชันหรือเมท็อดใด ๆ ต่างจากสายอักขระอย่าง 'strtoupper' หรืออาร์เรย์อย่าง [$obj, 'method'] ตรงที่สัญลักษณ์จะถูกตรวจสอบตอนสร้าง และเครื่องมือวิเคราะห์แบบสถิตติดตามชนิดได้ตลอดทั้งสาย สำหรับเมท็อดของอินสแตนซ์ โคลเชอร์จะจับอ็อบเจกต์ไว้ ส่วนเมท็อดแบบสแตติกจะจับเฉพาะคลาส
<?php
declare(strict_types=1);
$words = ['php', 'rust', 'go'];
$upper = strtoupper(...);
var_dump($upper('php'));
print_r(array_map(strtoupper(...), $words));
final class Mailer
{
public function send(string $to): bool
{
return $to !== '';
}
public static function ping(): string
{
return 'pong';
}
}
$mailer = new Mailer();
$send = $mailer->send(...);
$ping = Mailer::ping(...);
var_dump($send('dev@example.org'), $ping());