27 lines
794 B
PHP
27 lines
794 B
PHP
<?php
|
|
|
|
namespace App\DTOs;
|
|
|
|
abstract class CommonDTO
|
|
{
|
|
public function __construct() {}
|
|
|
|
public function __get(string $name)
|
|
{
|
|
// 1. 속성이 클래스에 실제로 존재하는지 확인
|
|
if (property_exists($this, $name)) {
|
|
// 2. 속성이 존재하면 값을 반환 (PHP 7.4 이상의 타입 속성 덕분에 null 병합 연산자 사용 불필요)
|
|
return $this->{$name};
|
|
}
|
|
// 3. 속성이 존재하지 않으면 오류 발생 (일반적인 Entity/DTO 접근 방식)
|
|
throw new \BadMethodCallException(
|
|
"Undefined property or method: " . static::class . "::{$name}"
|
|
);
|
|
}
|
|
public function toArray(): array
|
|
{
|
|
// $this가 가진 모든 public 프로퍼티와 그 값을 배열로 반환합니다.
|
|
return get_object_vars($this);
|
|
}
|
|
}
|