Skip to content
Advertisement

How to autoload custom annotation classes without using AnnotationRegistry?

I’m using Doctrine Annotations library (not the whole Doctrine, only annotations) and want to make a custom annotation class.

composer.json:

JavaScript

index.php:

JavaScript

entities/MyClass.php

JavaScript

annotations/TestAnnotation.php

JavaScript

It gives me the following error:

JavaScript

The only solution i found on the Internet is to use AnnotationRegistry::registerLoader or something similar, but it is deprecated so i’d like to solve the problem in another way.

Advertisement

Answer

One way to work around registering a loader is explicit require_once of all files with custom annotations somewhere during application’s bootstrap (such approach was used in the MongoDB ODM but was dropped).

In the next major version annotations will rely on autoloading so no code will be required for the setup. To have a future-proof code you could use:

JavaScript

You could explicitly pass Composer’s autoloader but class_exists will work just fine given Composer’s autoloader is already in use.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement