shlogg · Early preview
David Duymelinck @xwero

PHP Attributes In Action: Simplifying Code With Built-in Metadata

Exploring PHP Attributes: Adding metadata to classes & methods without libraries, using built-in attributes like #[Route] & #[Deprecated].

I'm not sure how big of a series I want to make this. But here we go.
The target of the series is to show php features using as few libraries as possible.
  
  
  Theory

Attributes are the php build-in way to add metadata to classes and methods.

class PostsController
{
    #[Route("/api/posts/{id}", methods: ["GET"])]
    public function get($id) { /* ... */ }
}

    
    

    
    




Before php 8.0 we were using docstrings to do the same thing.


class PostsController
{
    /**
     * @Route("/api/posts/{id}", methods={"GET"})
     */
    public function get($id) { /* ... */ }
}...