When building plugins in frameworks like Drupal or Symfony, you need a way to tell the system how your plugin should behave. Developers have traditionally used annotations—special comments in the code—to provide this information. But with PHP 8, a new feature called PHP attributes offers a better way to handle plugin metadata.
Annotations have been around for a long time, especially in Drupal. They help define things like plugin types and settings. However, PHP attributes are a newer, cleaner method that’s built right into PHP itself. This makes them easier to use and less error-prone.
In this article, we’ll look at both annotations and PHP attributes. We’ll explain how they work, how they’re different, and why PHP attributes might be the future of plugin development. Let’s dive in!

What are annotations
Annotations are a way to add extra information to your code using comments. These special comments tell the system how to treat your plugin. You’ve probably seen them in Drupal if you’ve worked with plugins before.
Here’s an example:
/**
* @Plugin(
* id = "custom_block",
* admin_label = @Translation("Custom Block"),
* )
*/
class CustomBlock extends BlockBase {
// Plugin logic here
}
In this case, the @Plugin annotation gives Drupal important details, like the plugin’s ID and label. It’s useful, but since annotations are just comments, they’re not enforced by PHP itself. This means there’s more room for mistakes.
Why use annotations
Annotations make it easy to find and manage plugin metadata because the information is stored right inside the class that implements the plugin. Everything you need is in the same file. This makes it simple to copy an existing annotation and create a new custom plugin.
Annotations also support complex data structures. You can even specify which strings need to be translated. Many plugins come with their own custom annotation class. This class helps set default values for the metadata and provides clear documentation on how to use it.
What are plugins
Plugins are small, reusable pieces of code that perform specific tasks in a larger system. In frameworks like Drupal, they help add functionality without changing the core code. For example, a Block plugin in Drupal controls what content appears in different sections of a webpage.
Annotations in plugins
In Drupal, most plugins use annotations to register themselves and describe their metadata. Here are some key types of plugins provided by Drupal core:
- Block Plugins: These define content blocks that can be placed in different regions of your site. You can find many examples in the /src/Plugin/Block/ directory.
- Field Plugins: This category includes field formatters and field widgets, allowing you to create custom fields for your content types. Check the /src/Plugin/Field/ directory for examples.
- Views Plugins: These plugins extend the Views module, which is used to display lists and grids of content. You can find examples in the /src/Plugin/views/ directory.
- Condition Plugins: Used for controlling block visibility in the core, these plugins determine when blocks should be shown or hidden based on specific conditions.
- Migrate Plugins: These include source, process, and destination plugins that are used during data migrations. They help manage how data is imported into Drupal.
These plugins play a vital role in Drupal’s flexibility and allow developers to customize and extend functionality easily.
How to add annotations
Adding annotations to your plugins in Drupal is straightforward.
- Define Your Plugin Class
- Use Docblock Comments
- Include Required Field
- Clear Cache:
To annotate a class as a plugin, add code like the following to the docblock just before the class declaration.
Syntax of Annotations:
/**
* @ContentEntityType(
* id = "comment",
* label = @Translation("Comment"),
* ...
* base_table = "comment"
* )
*/
What are PHP attributes in Drupal
PHP attributes, introduced in PHP 8, offer a better way to handle metadata. Unlike annotations, they are built right into PHP, making them more reliable. You don’t have to rely on comments anymore. Attributes make your code cleaner and easier to read by eliminating the need for bulky comments.
Attributes code style
When using PHP attributes, it’s important to follow a consistent code style for clarity and maintainability. Here are some key guidelines:
- Placement: Place attributes after the DocBlock comment. They should be the first line of code in that block.
- Syntax: Use square brackets [] for attributes. Leave no spaces before and after the #[ and ] braces
- Parameter Formatting: Use the same style for function calls: place a comma immediately after each parameter and include a space before the next parameter, like this: "first", "second". [Not for Drupal]
- Consistency: Stick to a consistent naming convention for your attributes, using camel case or snake case as appropriate for your project.
- Readability: Keep attribute definitions clear and concise. Avoid excessive complexity to maintain readability.
Syntax of Attributes:
#[Block(
id: "techx_block_attributes",
admin_label: new TranslatableMarkup("TechX Block Attributes")
)]
Annotations VS PHP Attributes: Key differences
Syntax:
- Annotations use docblock comments (e.g., /** ... */). They are part of the comments in the code.
- PHP Attributes use a native PHP syntax with square brackets (e.g., #[AttributeName]). They are part of the code itself.
Version Support:
- Annotations have been around for a long time and are widely used in Drupal 7 and 8.
- PHP Attributes were introduced in PHP 8, so they are used in Drupal 10 and later.
Performance:
- Annotations require parsing through comments, which can impact performance during plugin discovery.
- PHP Attributes are natively supported by PHP, making them faster to read and process.
Readability:
- Annotations can become cluttered, especially with multiple lines of metadata.
- PHP Attributes offer a cleaner, more concise way to define metadata, improving code readability.
Error Handling:
- Annotations can be prone to syntax errors or incorrect formatting since they are just comments.
- PHP Attributes are checked by the PHP parser, reducing the chance of errors and improving type safety.
Discovery:
- Annotations rely on specific comment formatting for discovery, which can lead to inconsistencies.
- PHP Attributes provide a standardized way for the system to recognize and handle plugins, ensuring more reliable discovery.
Annotation VS PHP Attributes: Code style
Example 1:
Annotation style

PHP Attributes Style

Example 2:
Annotation style

PHP Attributes style

Why use PHP Attributes
PHP attributes offer significant advantages that enhance both the development experience and the quality of the code in Drupal and other PHP applications.
- Simplified Syntax: PHP attributes are easier to read and write.
- Type Safety: They catch errors at compile time, reducing bugs.
- Improved Performance: Attributes are processed natively, speeding up execution.
- Centralized Configuration: Metadata is embedded directly within the code.
- Enhanced Readability: The relationship between code and metadata is clearer.
- Better Framework Integration: They provide a standardized way for frameworks to recognize plugins.
- Reduced Boilerplate: Less boilerplate code means more focus on functionality.
- Easier Refactoring: Updating metadata is simpler and less error-prone.
- Future-Proofing: Adopting attributes prepares your code for PHP’s evolution.
- Support for Complex Data: Attributes can handle advanced data structures easily.
Final thoughts
Using PHP attributes to add metadata right into your code is a smart move. They cut down on boilerplate, make maintenance easier, and keep everything organized. They also offer type safety and improve readability. Attributes help your code work better with the framework. As PHP continues to evolve, attributes will become even more important for writing clear and efficient code. No matter the size of your project, using attributes can really enhance the quality and manageability of your codebase.
Need an expert Drupal development company to handle this for you? Our team is just a message away!