Is it possible to combine annotations together with attributes in PHP 8.0
Asked Answered
F

2

6

Is it possible to combine annotations together with attributes like:

    /**
     * @Gedmo\Timestampable(on="create")
     */
    #[ORM\Column(type: 'datetime')]
    private ?DateTime $createdAt;
Forta answered 11/11, 2021 at 16:2 Comment(7)
What do you want to achieve with this? The language itself isn't going to stop you, so it's a question of what a specific tool is going to read. I think more details are needed before anyone can give you a useful answer.Eunuchize
My question is if the PHP docblock is ignored when php 8.0 attributes are usedForta
I get that, but my question in response is ignored by what? Neither docblock annotations nor attributes do anything unless something actively reads them, so different libraries might or might not read both.Eunuchize
In this case it’s an example of using annotations and attributes to declare doctrine (ORM) fields.Forta
So, the question is, does Doctrine ORM support reading both annotations and attributes in the same project? That can probably be discovered by looking at their documentation (or, for that matter, by testing it with some existing code).Eunuchize
I’ve tested and it seems working just needed confirmation ☺️Forta
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.Mor
S
2

The answer is yes: at least some declarations (eg. routing, security and Doctrine) work side by side in different formats. So there is no hard rule preventing this.

I'm not sure however if there aren't some edge cases where this could backfire, and if you can you should try to keep to one format. There's Rector tool that can handle converting those automatically. In my experience it's sometimes hard to find a proper rule for Rector but there is a nice playground (https://getrector.com/demo) and good support on GitHub.

Stave answered 11/8, 2023 at 10:29 Comment(1)
Rector combine annotations with attributesBeauteous
C
0

In case of Doctrine, you cannot mix annotation and attribute:

https://github.com/doctrine/orm/discussions/8925

doctrine:
    orm:
        entity_managers:
            default:
                mappings:
                    App:
                        type: attribute

Some software like Sylius, add both annotation and attribute in their code:

/**
 * @ORM\Entity
 * @ORM\Table(name="sylius_admin_user")
 */
#[ORM\Entity]
#[ORM\Table(name: 'sylius_admin_user')]
class AdminUser extends BaseAdminUser
{

}
Credits answered 12/10 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.