Phalcon introduced the first annotations parser component written in C for PHP. The Phalcon\Annotations namespace contains general purpose components that offer an easy way to parse and cache annotations in PHP applications.
Configurations
Annotation Settings
Define which adapter you want to use simply by setting the ANNOTATIONS_DRIVER environment variable. You can choose between memory, apcu, file, memcached and redis.
# Using Memory CacheANNOTATIONS_DRIVER=memory# Using Apcu CacheANNOTATIONS_DRIVER=apcu# Using File CacheANNOTATIONS_DRIVER=file# Using MemcachedANNOTATIONS_DRIVER=memcached# Using Redis ServerANNOTATIONS_DRIVER=redis
Good to know: You can define the annotations driver as well as the php adapter class to use. You can also define advanced configurations for each driver.
# Annotations Settings of the `memory` driverANNOTATIONS_DRIVER=memoryANNOTATIONS_APCU_ADAPTER=\Phalcon\Annotations\Adapter\Memory
# Annotations Settings of the `apcu` driverANNOTATIONS_DRIVER=apcuANNOTATIONS_APCU_ADAPTER=\Phalcon\Annotations\Adapter\Apcu
# Annotations Settings of the `file` driverANNOTATIONS_DRIVER=fileANNOTATIONS_STREAM_ADAPTER=\Phalcon\Annotations\Adapter\StreamANNOTATIONS_STREAM_DIR=<root>/private/cache/annotations
# Annotations Settings for the `memcached` driverANNOTATIONS_DRIVER=memcachedANNOTATIONS_MEMCACHED_ADAPTER=\Phalcon\Annotations\Adapter\MemcachedANNOTATIONS_MEMCACHED_HOST=127.0.0.1ANNOTATIONS_MEMCACHED_PORT=11211ANNOTATIONS_MEMCACHED_WEIGHT=100
# Annotations Settings for the `redis` driverANNOTATIONS_DRIVER=redisANNOTATIONS_REDIS_ADAPTER=\Phalcon\Annotations\Adapter\RedisANNOTATIONS_REDIS_DEFAULT_SERIALIZER=1ANNOTATIONS_REDIS_LIFETIME=3600ANNOTATIONS_REDIS_SERIALIZER=ANNOTATIONS_REDIS_HOST=127.0.0.1ANNOTATIONS_REDIS_PORT=6379ANNOTATIONS_REDIS_INDEX=1ANNOTATIONS_REDIS_AUTH=ANNOTATIONS_REDIS_PERSISTENT=0ANNOTATIONS_REDIS_SOCKET=
Usage
Annotations are read from docblocks in classes, methods and properties. An annotation can be placed at any position in the docblock.
// if the class is aware of injections$annotations =$this->annotations;// if the container is present$annotations =$this->di->get('annotations');// to access the shared db service$annotations =Di::getDefault()->get('annotations');