Laravel's PHP attributes

Vincent Bergeron • January 2, 2025

Happy New Year!

Lately I've been quite excited by Laravel's PHP attributes.

Attributes offer the ability to add structured, machine-readable metadata information on declarations in code: Classes, methods, functions, parameters, properties and class constants can be the target of an attribute.

Source: https://www.php.net/manual/en/language.attributes.overview.php

Laravel offers some attributes out-of-the-box and I found a very good article on X that describes them all.

My favourite one (and the one I use the most) is #[Config(...)]

Before, you would have to initialize the property yourself:

<?php

class Service {
    private string $apiKey;

    public function __construct()
    {
        $this->apiKey = config('services.api_key');
    }
}

Now, you can use the Config attribute to inject the configuration value automatically and leverage PHP's constructor property promotion at the same time.

<?php

use Illuminate\Container\Attributes\Config;

class Service {
    public function __construct(
        #[Config('services.api_key')] private string $apiKey,
    )
    {
        //
    }
}

Feel free to take a look at this blog post for a more in-depth look at all attributes Laravel (and Livewire) has to offer.