【发布时间】:2023-03-27 00:10:01
【问题描述】:
我正在使用 Doctrine Annotations 库(不是整个 Doctrine,只有注释)并且想要制作一个自定义注释类。
composer.json:
{
"require": {
"doctrine/annotations": "^1.6"
},
"autoload": {
"psr-4": {
"annotations\\": "annotations",
"entities\\": "entities"
}
}
}
index.php:
<?php
require 'vendor/autoload.php';
use Doctrine\Common\Annotations\AnnotationReader;
$annotationReader = new AnnotationReader();
$reflectionClass = new ReflectionClass(entities\MyClass::class);
$classAnnotations = $annotationReader->getClassAnnotations($reflectionClass);
var_dump($classAnnotations);
entities/MyClass.php
<?php
namespace entities;
use annotations\TestAnnotation;
/**
* @TestAnnotation("123")
*/
class MyClass
{
}
注解/TestAnnotation.php
<?php
namespace annotations;
/**
* @Annotation
* @Target("CLASS")
*/
final class TestAnnotation
{
/**
* @var string
*/
public $value;
}
它给了我以下错误:
[Semantical Error] The annotation "@annotations\TestAnnotation" in class entities\MyClass does not exist, or could not be auto-loaded.
我在 Internet 上找到的唯一解决方案是使用 AnnotationRegistry::registerLoader 或类似的东西,但它已被弃用,所以我想以另一种方式解决问题。
【问题讨论】:
标签: php doctrine-orm annotations composer-php autoload