NotionCommotion Posted March 1, 2021 Share Posted March 1, 2021 The following script came from using-custom-paths. It appears that these are PHP 8's new attributes, correct? <?php // api/src/Entity/Question.php #[ApiResource( subresourceOperations: [ 'api_questions_answer_get_subresource' => [ 'method' => 'GET', 'path' => '/questions/{id}/all-answers', ], ], )] class Question { } I currently am using PHP7 and will need to convert them. Does the following look correct? Thanks /** * @ApiResource( * subresourceOperations = { * "api_questions_answer_get_subresource" = { * "method" = "GET", * "path" = "/questions/{id}/all-answers" * } * } * ) */ Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/ Share on other sites More sharing options...
requinix Posted March 1, 2021 Share Posted March 1, 2021 28 minutes ago, NotionCommotion said: Does the following look correct? Depends what is reading the docblocks and parsing the annotations. PHP 8's attributes are a completely new thing. There is no compatibility between them and docblock annotations. There is no sort of conversion process to translate between them. Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1584823 Share on other sites More sharing options...
NotionCommotion Posted March 1, 2021 Author Share Posted March 1, 2021 3 hours ago, requinix said: Depends what is reading the docblocks and parsing the annotations. PHP 8's attributes are a completely new thing. There is no compatibility between them and docblock annotations. There is no sort of conversion process to translate between them. Per https://www.php.net/releases/8.0/en.php#attributes, it sure sounds like they are just a different way to do the same thing. What am I missing? Thanks Quote Instead of PHPDoc annotations, you can now use structured metadata with PHP's native syntax. PHP7 class PostsController { /** * @Route("/api/posts/{id}", methods={"GET"}) */ public function get($id) { /* ... */ } } PHP8 class PostsController { #[Route("/api/posts/{id}", methods: ["GET"])] public function get($id) { /* ... */ } } Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1584827 Share on other sites More sharing options...
requinix Posted March 1, 2021 Share Posted March 1, 2021 50 minutes ago, NotionCommotion said: Per https://www.php.net/releases/8.0/en.php#attributes, it sure sounds like they are just a different way to do the same thing. What am I missing? Thanks They're the same concept, both acting as ways to add additional information to a thing. But they have very different implementations. Quote Instead of PHPDoc annotations, you can now use structured metadata with PHP's native syntax. What they (probably) did (more or less) is reuse the same backend "Route" thing for both annotations and attributes. Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1584828 Share on other sites More sharing options...
NotionCommotion Posted March 1, 2021 Author Share Posted March 1, 2021 Still confused on the difference. Is it fair to say that all annotations can be represented by attributes but not all attributes can be represented by annotations? Also, does it look like I correctly reused the same backend "ApiResource" thing for both annotations and attributes? Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1584829 Share on other sites More sharing options...
requinix Posted March 2, 2021 Share Posted March 2, 2021 1 hour ago, NotionCommotion said: Is it fair to say that all annotations can be represented by attributes but not all attributes can be represented by annotations? Is it fair to say that all apples can be substituted for pears but not all pears can be substituted for apples? 1 hour ago, NotionCommotion said: Also, does it look like I correctly reused the same backend "ApiResource" thing for both annotations and attributes? I don't know. I'm telling you there is no conversion process because annotations and attributes are two different things. As far as PHP is concerned, docblock annotations are just comments. Just comments. Nothing else. The whole reason those things work is because some library is using reflection to grab the docblock string for a method or property, then parse it for @things. Different libraries parse different ways. Meanwhile attributes are an actual language feature. Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1584830 Share on other sites More sharing options...
NotionCommotion Posted March 2, 2021 Author Share Posted March 2, 2021 2 hours ago, requinix said: Is it fair to say that all apples can be substituted for pears but not all pears can be substituted for apples? No, but all apples are fruit but not all fruits are apples. The same goes for pears. Not sure about tomatoes... Can PHP "native" attributes be used to reflect all comment annotations? Not sure whether the word "native" is applicable... 2 hours ago, requinix said: II don't know. I'm telling you there is no conversion process because annotations and attributes are two different things. As far as PHP is concerned, docblock annotations are just comments. Just comments. Nothing else. The whole reason those things work is because some library is using reflection to grab the docblock string for a method or property, then parse it for @things. Different libraries parse different ways. Meanwhile attributes are an actual language feature. If someone knew the context of the attributes, couldn't one determine the equivalent annotations? For this use case, the use case is known, so why can't the equivalent annotations be derived? Or do I just need to upgrade to PHP8? Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1584832 Share on other sites More sharing options...
requinix Posted March 2, 2021 Share Posted March 2, 2021 5 hours ago, NotionCommotion said: No, Exactly. 5 hours ago, NotionCommotion said: Can PHP "native" attributes be used to reflect all comment annotations? Not sure whether the word "native" is applicable... If people write the attribute classes, sure. 5 hours ago, NotionCommotion said: If someone knew the context of the attributes, couldn't one determine the equivalent annotations? A human being, sure. 5 hours ago, NotionCommotion said: For this use case, the use case is known, so why can't the equivalent annotations be derived? By PHP? Because 5 hours ago, NotionCommotion said: Or do I just need to upgrade to PHP8? Given how new PHP 8 is, it would be really stupid of that third party to not support traditional annotations as well. Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1584833 Share on other sites More sharing options...
NotionCommotion Posted March 2, 2021 Author Share Posted March 2, 2021 As indicated by my initial post, the api-platform documentation only shows PHP 8 attributes, but I don't have PHP 8 and therefore need to convert them. But there is no way to convert them. Unless by a human. Maybe. 16 hours ago, requinix said: Depends what is reading the docblocks and parsing the annotations. PHP 8's attributes are a completely new thing. There is no compatibility between them and docblock annotations. There is no sort of conversion process to translate between them. 3 hours ago, requinix said: human being, sure. Given how new PHP 8 is, it would be really stupid of that third party to not support traditional annotations as well. Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1584835 Share on other sites More sharing options...
requinix Posted March 2, 2021 Share Posted March 2, 2021 6 hours ago, NotionCommotion said: As indicated by my initial post, the api-platform documentation only shows PHP 8 attributes, but I don't have PHP 8 and therefore need to convert them. Either that or the docs are misleading. Check for an older version, look for existing code out in the wild to see what it does, see if there's something that points to docblock annotations instead. Or worst case, read the source. Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1584842 Share on other sites More sharing options...
NotionCommotion Posted March 3, 2021 Author Share Posted March 3, 2021 18 hours ago, requinix said: Either that or the docs are misleading. Check for an older version, look for existing code out in the wild to see what it does, see if there's something that points to docblock annotations instead. Or worst case, read the source. Okay. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1584863 Share on other sites More sharing options...
NotionCommotion Posted July 29, 2021 Author Share Posted July 29, 2021 On 3/1/2021 at 11:25 AM, requinix said: Depends what is reading the docblocks and parsing the annotations. PHP 8's attributes are a completely new thing. There is no compatibility between them and docblock annotations. There is no sort of conversion process to translate between them. I just discovered that there is a conversion process to translate annotations to attributes and initial testing is very promising. Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1588706 Share on other sites More sharing options...
kicken Posted July 30, 2021 Share Posted July 30, 2021 1 hour ago, NotionCommotion said: I just discovered that there is a conversion process to translate annotations to attributes and initial testing is very promising. There may be conversion processes for specific cases, but there isn't a general conversion process. The docblock annotations where not any kind of standard/official thing, just a popular idea that people copied. Each specific implementation will have it's own requirements when it comes to converting them to an official PHP attribute implementation or vice-versa. Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1588711 Share on other sites More sharing options...
NotionCommotion Posted July 30, 2021 Author Share Posted July 30, 2021 Understood kicken and did not mean to refute that there is no universal conversion process but only that conversion processes can be created on a case by case basis. Quote Link to comment https://forums.phpfreaks.com/topic/312232-php-8s-attribute-feature-converted-to-php-7-compatible-attributes/#findComment-1588713 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.