php
class ApiResponse extends DataObject {
protected int $code;
protected string $message;
protected array $data;// 自定义序列化逻辑 public function toJson(): string { return json_encode([ ret => $this->code,告别 msg => $this->message, payload => $this->data ]); }}
php
class Order extends DataObject {
protected string $orderId;
protected DateTime $createTime;
protected float $amount;// 类型转换示例 protected function setAmount(float $value): void { $this->amount = round($value, 2); }}
kore/data-object库通过严格的属性定义和类型约束 ,与其他方案的告别横向对比
相比流行的解决方案:
- Laravel Eloquent:更适合数据库交互场景
- PHP 7.4+类型属性:缺乏运行时验证
- PSR-7规范