【发布时间】:2018-07-20 16:29:39
【问题描述】:
我正在尝试使用 Symfony4 读取注释,但看起来有些东西不起作用!
我要阅读的课程:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\OAuthClientRepository")
*/
class OAuthClient {
{
/**
* @Assert\NotBlank()
* @ORM\Column(type="string")
*/
protected $name;
}
我用来阅读注释的代码:
<?php
namespace App\Test;
use Doctrine\Common\Annotations\SimpleAnnotationReader as DocReader;
/**
* Helper AnnotationReader
*/
class AnnotationReader
{
/**
* @param $class
* @return null|\StdClass
*/
public static function getClass($class)
{
$reader = new DocReader;
$reflector = new \ReflectionClass($class);
return $reader->getClassAnnotations($reflector);
}
/**
* @param $class
* @param $property
* @return array
*/
public static function getProperty($class, $property)
{
$reader = new DocReader;
$reflector = new \ReflectionProperty($class, $property);
return $reader->getPropertyAnnotations($reflector);
}
/**
* @param $class
* @param $method
* @return array
*/
public static function getMethod($class, $method)
{
$reader = new DocReader;
$reflector = new \ReflectionMethod($class, $method);
return $reader->getMethodAnnotations($reflector);
}
}
当我调用时得到空数组:
App\Test\AnnotationReader::getClass(App\Entity\OAuthClient::class);
App\Test\AnnotationReader::getProperty(App\Entity\OAuthClient::class, 'name');
我做错了什么? 阅读注释的最佳方式是什么?
我正在寻找在类属性上使用的验证。
感谢您的帮助!
【问题讨论】:
标签: php annotations symfony4