php判断反射类库是否可用,Php使用Hack中的反射获取通用类型

我正在使用HHVM探索Hack,并且正在使用泛型.我有以下基本存储库:

class BaseRepository{

public function __construct(T $model){

...

}

}

然后我有子类UserRepository像这样:

class UserRepository extends BaseRepository {

}

我想要做的是使用反射在运行时获取T的类型.

我尝试了以下方法:

$reflectionClass = new ReflectionClass('UserRepository');

$parameters = $reflectionClass->getConstructor()->getParameters();

var_dump($parameters);

输出以下内容:

array(1) {

[0]=>

object(ReflectionParameter)#854 (2) {

["info"]=>

array(9) {

["index"]=>

int(0)

["name"]=>

string(5) "model"

["type"]=>

string(0) ""

["type_hint"]=>

string(1) "T"

["function"]=>

string(11) "__construct"

["class"]=>

string(36) "BaseRepository"

["nullable"]=>

bool(true)

["attributes"]=>

array(0) {

}

["is_optional"]=>

bool(false)

}

["name"]=>

string(5) "model"

}

}

然后,我遍历参数并调用:

$parameter-> getClass()

返回空值.

是否可以使用反射在运行时获取T的类型?如果是这样,我该怎么做?

解决方法:

不幸的是,现在无法在运行时获取实际的遗传类型. HHVM具有针对它们的类​​型擦除语义,这意味着我们实际上在运行代码时不知道T的特定类型是什么.但是,能够做到这一点通常会很有用,并且我们已经考虑了如何添加它,称为“ reifiedgenerics”.但这是一个非常复杂的,涉及变更的事情,因此您不应该期望它很快出现.抱歉!

标签:hacklang,reflection,generics,hhvm,php

来源: https://codeday.me/bug/20191121/2049592.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值