Matt Glaman: Writing Drush commands with PHP attributes

Drupal CMS

I recently did a deep dive into command authoring with Drush, which is where I discovered two amazing new features: auto-discovery of commands via autoloading and the addition of attributes for defining your commands.

What are attributes? Attributes were added in PHP 8, and the overview on the PHP website is a great resource. So, if you are new to PHP 8 and have been living on PHP 7.4, still, or haven’t tried out PHP 8’s coolest feature, this blog will be a great introduction! If you have ever worked with Java, they’re similar to annotations. I first experienced attributes when I was learning Rust. 

First, what does an attribute look like? Let’s take a given PHPDoc with annotations. Remember, annotations are prefixed with the @ symbol in a document block.