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 On 3/1/2021 at 6:55 PM, NotionCommotion said: Does the following look correct? Expand 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 On 3/1/2021 at 7:25 PM, 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. Expand 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. Expand 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 On 3/1/2021 at 10:38 PM, 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 Expand 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. Expand 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 On 3/1/2021 at 11:52 PM, NotionCommotion said: Is it fair to say that all annotations can be represented by attributes but not all attributes can be represented by annotations? Expand Is it fair to say that all apples can be substituted for pears but not all pears can be substituted for apples? On 3/1/2021 at 11:52 PM, NotionCommotion said: Also, does it look like I correctly reused the same backend "ApiResource" thing for both annotations and attributes? Expand 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 On 3/2/2021 at 1:13 AM, requinix said: Is it fair to say that all apples can be substituted for pears but not all pears can be substituted for apples? Expand 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... On 3/2/2021 at 1:13 AM, 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. Expand 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 On 3/2/2021 at 3:35 AM, NotionCommotion said: No, Expand Exactly. On 3/2/2021 at 3:35 AM, NotionCommotion said: Can PHP "native" attributes be used to reflect all comment annotations? Not sure whether the word "native" is applicable... Expand If people write the attribute classes, sure. On 3/2/2021 at 3:35 AM, NotionCommotion said: If someone knew the context of the attributes, couldn't one determine the equivalent annotations? Expand A human being, sure. On 3/2/2021 at 3:35 AM, NotionCommotion said: For this use case, the use case is known, so why can't the equivalent annotations be derived? Expand By PHP? Because On 3/2/2021 at 3:35 AM, NotionCommotion said: Or do I just need to upgrade to PHP8? Expand 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. On 3/1/2021 at 7:25 PM, 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. Expand On 3/2/2021 at 9:00 AM, 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. Expand 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 On 3/2/2021 at 12:14 PM, 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. Expand 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 On 3/2/2021 at 6:27 PM, 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. Expand 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 7:25 PM, 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. Expand 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 On 7/29/2021 at 10:53 PM, NotionCommotion said: I just discovered that there is a conversion process to translate annotations to attributes and initial testing is very promising. Expand 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.