gpt4 book ai didi

php - 如何获取学说实体属性的类型

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:56:36 24 4
gpt4 key购买 nike

实际上我有一个学说实体,我需要动态填充它的属性。

我希望能够做这样的事情:

$entity = new Entity();
$reflect = new ReflectionClass($entity);
// $fields is an array whihch contain the entity name as the array key and the value as the array value
foreach ($fields as $key => $val)
{
if (!reflect->hasProperty($key)) {
var_dump('the entity does not have a such property');
continue;
}
if ( the type of $key is string ) {
// convert $value to utf8
} elseif ( the type of $key is integer) {
// do something else
} //....etc
}

我该怎么做?

最佳答案

找到 the solution thanks to @Yoshi .希望对你有帮助

use Doctrine\Common\Annotations\AnnotationReader;

$docReader = new AnnotationReader();
$entity = new Entity();
$reflect = new ReflectionClass($entity);
// $fields is an array whihch contain the entity name as the array key and the value as the array value
foreach ($fields as $key => $val)
{
if (!reflect->hasProperty($key)) {
var_dump('the entity does not have a such property');
continue;
}
$docInfos = $docReader->getPropertyAnnotations($reflect->getProperty($key));
if ( $docInfos[0]->type === 'string' ) {
// convert $value to utf8
} elseif ( $docInfos[0]->type === 'integer' ) {
// do something else
} //....etc
}

关于php - 如何获取学说实体属性的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27293233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com