Using Doctrine Annotations for creating custom annotations fails











up vote
0
down vote

favorite












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 MyCompanyAnnotationsAnnotation;

use DoctrineCommonAnnotationsAnnotation;

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

<?php
namespace MyCompanyAnnotations;

use MyCompanyAnnotationsAnnotation as MYC;

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

/**
* @MYCType(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 DoctrineCommonAnnotationsAnnotationReader;
use DoctrineCommonAnnotationsCachedReader;
use DoctrineCommonCacheArrayCache;
use MyCompanyAnnotationsPerson;

$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 'DoctrineCommonAnnotationsAnnotationException' with message '[Semantical Error] The annotation "@MyCompanyAnnotationsAnnotationType" in property MyCompanyAnnotationsPerson::$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.










share|improve this question


























    up vote
    0
    down vote

    favorite












    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 MyCompanyAnnotationsAnnotation;

    use DoctrineCommonAnnotationsAnnotation;

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

    <?php
    namespace MyCompanyAnnotations;

    use MyCompanyAnnotationsAnnotation as MYC;

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

    /**
    * @MYCType(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 DoctrineCommonAnnotationsAnnotationReader;
    use DoctrineCommonAnnotationsCachedReader;
    use DoctrineCommonCacheArrayCache;
    use MyCompanyAnnotationsPerson;

    $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 'DoctrineCommonAnnotationsAnnotationException' with message '[Semantical Error] The annotation "@MyCompanyAnnotationsAnnotationType" in property MyCompanyAnnotationsPerson::$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.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 MyCompanyAnnotationsAnnotation;

      use DoctrineCommonAnnotationsAnnotation;

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

      <?php
      namespace MyCompanyAnnotations;

      use MyCompanyAnnotationsAnnotation as MYC;

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

      /**
      * @MYCType(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 DoctrineCommonAnnotationsAnnotationReader;
      use DoctrineCommonAnnotationsCachedReader;
      use DoctrineCommonCacheArrayCache;
      use MyCompanyAnnotationsPerson;

      $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 'DoctrineCommonAnnotationsAnnotationException' with message '[Semantical Error] The annotation "@MyCompanyAnnotationsAnnotationType" in property MyCompanyAnnotationsPerson::$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.










      share|improve this question













      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 MyCompanyAnnotationsAnnotation;

      use DoctrineCommonAnnotationsAnnotation;

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

      <?php
      namespace MyCompanyAnnotations;

      use MyCompanyAnnotationsAnnotation as MYC;

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

      /**
      * @MYCType(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 DoctrineCommonAnnotationsAnnotationReader;
      use DoctrineCommonAnnotationsCachedReader;
      use DoctrineCommonCacheArrayCache;
      use MyCompanyAnnotationsPerson;

      $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 'DoctrineCommonAnnotationsAnnotationException' with message '[Semantical Error] The annotation "@MyCompanyAnnotationsAnnotationType" in property MyCompanyAnnotationsPerson::$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.







      php annotations






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 12:54









      altralaser

      6581630




      6581630
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          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';
          DoctrineCommonAnnotationsAnnotationRegistry::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






          share|improve this answer























          • Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
            – altralaser
            Nov 8 at 13:33










          • @altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
            – iainn
            Nov 8 at 13:44











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208170%2fusing-doctrine-annotations-for-creating-custom-annotations-fails%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          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';
          DoctrineCommonAnnotationsAnnotationRegistry::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






          share|improve this answer























          • Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
            – altralaser
            Nov 8 at 13:33










          • @altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
            – iainn
            Nov 8 at 13:44















          up vote
          0
          down vote



          accepted










          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';
          DoctrineCommonAnnotationsAnnotationRegistry::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






          share|improve this answer























          • Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
            – altralaser
            Nov 8 at 13:33










          • @altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
            – iainn
            Nov 8 at 13:44













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          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';
          DoctrineCommonAnnotationsAnnotationRegistry::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






          share|improve this answer














          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';
          DoctrineCommonAnnotationsAnnotationRegistry::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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 8 at 13:31

























          answered Nov 8 at 13:16









          iainn

          11.8k81929




          11.8k81929












          • Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
            – altralaser
            Nov 8 at 13:33










          • @altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
            – iainn
            Nov 8 at 13:44


















          • Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
            – altralaser
            Nov 8 at 13:33










          • @altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
            – iainn
            Nov 8 at 13:44
















          Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
          – altralaser
          Nov 8 at 13:33




          Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
          – altralaser
          Nov 8 at 13:33












          @altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
          – iainn
          Nov 8 at 13:44




          @altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
          – iainn
          Nov 8 at 13:44


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208170%2fusing-doctrine-annotations-for-creating-custom-annotations-fails%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Landwehr

          Reims

          Javascript gets undefined on array