Annotations

Configuration & usage

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 Cache
ANNOTATIONS_DRIVER=memory

# Using Apcu Cache
ANNOTATIONS_DRIVER=apcu

# Using File Cache
ANNOTATIONS_DRIVER=file

# Using Memcached
ANNOTATIONS_DRIVER=memcached

# Using Redis Server
ANNOTATIONS_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` driver
ANNOTATIONS_DRIVER=memory
ANNOTATIONS_APCU_ADAPTER=\Phalcon\Annotations\Adapter\Memory

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');

Sources

Last updated