在多个支付类共享这段生成的审计 Trait 前,对它进行审查。
生成由接口约束的审计标签,显式声明全部宿主需求,由使用方类负责构造,并让每个对象独立收集可编码事件。
php
<?php
interface Auditable
{
public function auditLabel(): string;
}
trait HasAudit
{
private static array $events = [];
public function __construct(private string $accountId) {}
public function auditLabel(): string
{
return $this->prefix . ':' . $this->accountId;
}
public function record(array $context): void
{
self::$events[] = json_encode($context);
}
public function events(): array { return self::$events; }
}
final class Payment implements Auditable
{
use HasAudit;
private string $prefix = 'payment';
}
生成代码仅作示例,不代表任何特定模型