Jump to content

Recommended Posts

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"
 *     }
 *  }
 * )
 */

 

  On 3/1/2021 at 6:55 PM, NotionCommotion said:

Does the following look correct?

Expand  

:psychic:

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.

  On 3/1/2021 at 7:25 PM, requinix said:

:psychic:

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) { /* ... */ }
}

 

  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.

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?

  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.

  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?

  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 :psychic:

 

  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.

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:

:psychic:

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  

 

  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.

  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

  • 4 months later...
  On 3/1/2021 at 7:25 PM, requinix said:

:psychic:

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.

  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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.