dongyue9864 2018-11-08 12:54
浏览 918
已采纳

使用Doctrine Annotations创建自定义注释失败

I'm trying to define and use custom annotations but I can't figure out the problem. The basis is doctrine/annotations and these are my own classes:

<?php
namespace MyCompany\Annotations\Annotation;

use Doctrine\Common\Annotations\Annotation;

/**
 * @Annotation
 * @Target("PROPERTY")
 */
final class Type
{
    /**
     * @Required
     *
     * @var string
     */
    public $name;
}

<?php
namespace MyCompany\Annotations;

use MyCompany\Annotations\Annotation as MYC;

class Person
{
    /**
     * @MYC\Type(name = "string")
     */
    private $firstName;

    /**
     * @MYC\Type(name = "string")
     */
    private $lastName;

    public function __construct($firstName, $lastName)
    {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }

    public function getFirstName()
    {
        return $this->firstName;
    }

    public function getLastName()
    {
        return $this->lastName;
    }
}

Now I want to read the annotations of all properties:

<?php
require __DIR__ . '/../vendor/autoload.php';

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Cache\ArrayCache;
use MyCompany\Annotations\Person;

$refClass = new ReflectionClass(Person::class);
$props = $refClass->getProperties();

foreach ($props as $prop) {
    $reader = new AnnotationReader();
    $annotationReader = new CachedReader(
        $reader, new ArrayCache()
    );
    $annotations = $annotationReader->getPropertyAnnotations(
        $prop
    );
    print_r($annotations);
}

If I run my test script, it fails with the following error:

Uncaught exception 'Doctrine\Common\Annotations\AnnotationException' with message '[Semantical Error] The annotation "@MyCompany\Annotations\Annotation\Type" in property MyCompany\Annotations\Person::$firstName does not exist, or could not be auto-loaded.'

What confuses me is the fact that the class name in the error message starts with a '@' character.

  • 写回答

1条回答 默认 最新

  • donglou1866 2018-11-08 13:16
    关注

    You're missing a call to register the annotations (both yours and any internal ones you're using). The easiest way to do this is to pass the Composer autoloader directly to Doctrine's registerLoader method:

    $loader = require_once 'vendor/autoload.php';
    Doctrine\Common\Annotations\AnnotationRegistry::registerLoader([$loader, "loadClass"]);
    

    at the top of your test script.

    This assumes that you're doing all of your autoloading through Composer but if not, there are methods available to register individual files or namespaces described here in the manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据