I recently bumped into trifle that bothers me about annotating in Symfony (not sure if it's also pure PHP thing).
New Route annotations looks like this:
#[Route('route', name: 'route_name')]
And when I want the same type of annotation for @throws
I can't find any way instead the old type that looks like this:
/**
* @throws Exception
*/
So I want the new type of annotations even for @throws
CodePudding user response:
The main idea behind attributes is to attach static metadata to our code (methods, properties, etc.). These metadata often include concepts such as "configuration". In your example, this attribute setup a route for your framework. With doctrine, you can setup your entity. A few years ago, there are no attributes, so developers used comments to declare them. PHP8 allow us to use attributes. Let's avoid to use comment (@Route).
Throw tag doesn't setup anything in your application. The exception can only be thrown by your code.
/**
* @throws Exception //this line has no impact on your code
*/
#[Route('route', name: 'route_name')] //this line has an impact by setting up your routes
public myAction() {
throw new Exception('foo'); // This line have an impact on your code
}
This is an information for developers (and valuable IDE) to help us to understand the code. So, there is no attribute version of the @Throw tag. @Throw tag "only" is a documentation, a PHPDoc, a comment :) Your code should mix Annotations in comments and Attributes