Search code examples
phpattributesphp-8

PHP 8: how to declare an associative array in attributes


I have this controller annotation :

/**
 * @Route("/", name="list", defaults={"_format": "html", "tag": ""})
 */

I want to convert it to PHP 8 attributes. But how to pass the associative array of the defaults parameter? This works:

#[Route("/", name: "list", defaults: [])]

But this isn't:

#[Route("/", name: "list", defaults: ["_format": "html", "tag": ""])]

How should I do?


Solution

  • OK, found the answer while writing the question, that was quite obvious. We have to use the PHP standard syntax:

    #[Route("/", name: "list", defaults: ["_format" => "html", "tag" => ""])]