Attributes

Starting with PHP 8, we can use attributes instead of annotations!

Objectiphy v2.0 remains backward compatible with PHP 7.4, which did not support attributes as part of the language. Attributes were introduced in PHP 8.0, and they essentially perform the same role that annotations performed before then.

Annotations were just comments in a doc block, had no officially defined syntax, and were completely ignored by PHP - they required a library to read and parse the doc block (typically, Doctrine's annotation reader). As of PHP 8, use of annotations is discouraged, as attributes are now part of the language and are parsed by PHP itself, following officially recognised syntax. This makes them more performant and it becomes much easier to identify syntax errors and prevent bugs.

Objectiphy will continue to support reading annotations indefinitely, but if you are creating new entities, you are strongly encouraged to use attributes instead. If attributes are present on an entity, Objectiphy will not bother checking for annotations (to speed up processing), so do not try to use a mixture of both! In the pages that follow, descriptions are given of mapping definitions for both Objectiphy and Doctrine (just the Doctrine ones that Objectiphy can understand, which is not all of them), and both attribute and annotation syntax are shown in the examples.

Last updated