gpt4 book ai didi

symfony - 获取 set 属性中参数的类型

转载 作者:行者123 更新时间:2023-12-03 20:20:55 25 4
gpt4 key购买 nike

使用 Symfony 2 框架,我有来自数据库的带有注释的实体。

在我的特殊情况下,我使用实体,但我不知道我使用的是哪一个。

假设我有一个实体对象 $entity

$class = get_class($entity);
$reflect = new \ReflectionClass($class)
$properties = $reflect->getProperties();

foreach($properties as $property) {
$entity->{'set' .ucfirst(strtolower($property))}($some_value);
....
}

有没有办法知道每个属性,它期望什么类型的参数?使用注释?

最佳答案

理论上你可以这样做,但我不知道它会如何影响性能。
您可以为此使用 Doctrine AnnotationReader。

这是您可以执行的操作的代码示例。

use Doctrine\Common\Annotations\AnnotationReader;

$class = get_class($entity);
$reflect = new \ReflectionClass($class)
$properties = $reflect->getProperties();
$annotationReader = new AnnotationReader();

foreach($properties as $property) {
$reflectionProperty = new ReflectionProperty($class, $property);
$propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty);
var_dump($propertAnnotations);
....
}

除此之外,您可能需要查看 Symfony ProppertyAccesor 以获取和/或设置特定的对象值。
http://symfony.com/doc/current/components/property_access/introduction.html

关于symfony - 获取 set 属性中参数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36914882/

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