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

 

28 minutes ago, NotionCommotion said:

Does the following look correct?

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

3 hours ago, 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.

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

 

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.

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?

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.

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?

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

 

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.

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:

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

 

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.

 

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.

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

  • 4 months later...
On 3/1/2021 at 11:25 AM, 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.

I just discovered that there is a conversion process to translate annotations to attributes and initial testing is very promising.

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.

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.