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.