在 PHP 7.4 中获取类型 属性
Get type of typed property in PHP 7.4
我有一个 DTO
类型的 PHP 变量:
class CreateMembershipInputDto extends BaseDto
{
public bool $is_gift;
public int $year;
public string $name;
public \DateTime $shipping_date;
public ContactInputDto $buyer;
public ?ContactInputDto $receiver;
}
我正在尝试制作某种填充属性的自动映射器,但我需要检查变量的类型,但这似乎是不可能的。
class BaseDto
{
public function __construct($json)
{
$jsonArray = json_decode($json, true);
foreach($jsonArray as $key=>$value){
$type = gettype($this->$key);
if($type instanceof BaseDto)
$this->$key = new $type($value);
else
$this->$key = $value;
}
}
}
ContactInputDto:
class ContactInputDto extends BaseDto
{
public string $firstname;
public string $lastname;
public string $street_housenumber;
public string $postal_code;
public string $place;
public string $country;
public string $email;
public string $phone;
}
是否有可能使 "gettype($this->$key)"
行正常工作,而不 php 抛出以下错误:
Typed property App\Dto\CreateMembershipInputDto::$is_gift must not be accessed before initialization
虽然手册目前似乎没有记载,但 ReflectionProperty
to allow you to get the type. This is actually specified in the RFC for typed properties
中添加了一种方法
以下是您将如何使用它:
class CreateMembershipInputDto extends BaseDto {
public bool $is_gift;
public int $year;
public string $name;
public \DateTime $shipping_date;
public ContactInputDto $buyer;
public ?ContactInputDto $receiver;
}
class BaseDto
{
public function __construct($json)
{
$r = new \ReflectionClass(static::class); //Static should resolve the the actual class being constructed
$jsonArray = json_decode($json, true);
foreach($jsonArray as $key=>$value){
$prop = $r->getProperty($key);
if (!$prop || !$prop->getType()) { continue; } // Not a valid property or property has no type
$type = $prop->getType();
if($type->getName() === BaseDto::class) //types names are strings
$this->$key = new $type($value);
else
$this->$key = $value;
}
}
}
如果你想检查类型是否扩展 BaseDto
你将需要 (new \ReflectionClass($type->getName()))->isSubclassOf(BaseDto::class)
注意getName
指的是ReflectionNamedType::getName
. Prior to PHP 8 this was the only possible instance you could get $prop->getType()
however starting PHP 8 you may also get a ReflectionUnionType
包含多个类型
我有一个 DTO
类型的 PHP 变量:
class CreateMembershipInputDto extends BaseDto
{
public bool $is_gift;
public int $year;
public string $name;
public \DateTime $shipping_date;
public ContactInputDto $buyer;
public ?ContactInputDto $receiver;
}
我正在尝试制作某种填充属性的自动映射器,但我需要检查变量的类型,但这似乎是不可能的。
class BaseDto
{
public function __construct($json)
{
$jsonArray = json_decode($json, true);
foreach($jsonArray as $key=>$value){
$type = gettype($this->$key);
if($type instanceof BaseDto)
$this->$key = new $type($value);
else
$this->$key = $value;
}
}
}
ContactInputDto:
class ContactInputDto extends BaseDto
{
public string $firstname;
public string $lastname;
public string $street_housenumber;
public string $postal_code;
public string $place;
public string $country;
public string $email;
public string $phone;
}
是否有可能使 "gettype($this->$key)"
行正常工作,而不 php 抛出以下错误:
Typed property App\Dto\CreateMembershipInputDto::$is_gift must not be accessed before initialization
虽然手册目前似乎没有记载,但 ReflectionProperty
to allow you to get the type. This is actually specified in the RFC for typed properties
以下是您将如何使用它:
class CreateMembershipInputDto extends BaseDto {
public bool $is_gift;
public int $year;
public string $name;
public \DateTime $shipping_date;
public ContactInputDto $buyer;
public ?ContactInputDto $receiver;
}
class BaseDto
{
public function __construct($json)
{
$r = new \ReflectionClass(static::class); //Static should resolve the the actual class being constructed
$jsonArray = json_decode($json, true);
foreach($jsonArray as $key=>$value){
$prop = $r->getProperty($key);
if (!$prop || !$prop->getType()) { continue; } // Not a valid property or property has no type
$type = $prop->getType();
if($type->getName() === BaseDto::class) //types names are strings
$this->$key = new $type($value);
else
$this->$key = $value;
}
}
}
如果你想检查类型是否扩展 BaseDto
你将需要 (new \ReflectionClass($type->getName()))->isSubclassOf(BaseDto::class)
注意getName
指的是ReflectionNamedType::getName
. Prior to PHP 8 this was the only possible instance you could get $prop->getType()
however starting PHP 8 you may also get a ReflectionUnionType
包含多个类型