21 questions
0
votes
1
answer
82
views
How to reference current property value in class property attribute in PHP 8.4?
I've done a lot of Googling and may have missed this, there's so many examples of using the new Attribute feature in PHP but none of them show if this is possible. What I would like to be able to do ...
1
vote
1
answer
64
views
Do I still need getters and setters when using Entities via Attributes?
According to this page it is now possible and recommended to define entities without the use of getters and setters for the fields.
But when I define my Entity like this in "shopware/core": &...
0
votes
1
answer
277
views
PhpStorm: removed unused element warning if specific attributes are present
I'm currently working on a Symfony-based CMS which uses attributes to declare callbacks. Because of that the function is not called directly anywhere in the code. This causes PhpStorm to show a "...
4
votes
2
answers
312
views
PHP attributes : is it possible to use static function for a attribute parameter value?
I have a project that use attributes :
#[OperationQueryParam('id', IntegerOperation::class, requirements: new Numeric(min: 1, max: 2_147_483_647))]
Everything works fine but there are multiple ...
0
votes
1
answer
325
views
PHP attribute get target
Let's say I have the following classes:
class Bar{
public int $id;
}
class Boo{
public string $name;
}
class Foo{
#[ObjectAttr]
public Bar $barObject;
#[ObjectAttr]
public ...
3
votes
1
answer
1k
views
Method with a SerializedName attribute getting ignored when object is serialized with symfony serializer
I have a symfony serializer that i initialize this way :
$this->serializer = new Serializer(
[
new ArrayDenormalizer(),
new UidNormalizer(),
new BackedEnumNormalizer(),
...
2
votes
1
answer
420
views
How to create a preset of attributes using an attributes?
I'm using symfony 5.4 with php 8.1 and I would like to factor a set of attributes used for Swagger documentation, before each controllers method, I have, at least, these 5 attributes:
#[OA\Response(
...
0
votes
1
answer
2k
views
PHP use ENUM in Attributes
Look at the following code:
<?php
enum Types:string {
case A = 'a';
case B = 'b';
}
#[Attribute(Attribute::TARGET_CLASS)]
class MyAttribute {
public function __construct(public ...
0
votes
0
answers
334
views
Composer package to explicitly allow annotations with symfony's maker bundle?
I'm working with Bolt CMS on a project. It seems great so far, but it looks like the Bolt team has been a bit cautious about switching from annotation mappings to attribute mappings, as when I use ...
0
votes
1
answer
247
views
Custom validator for email doesn't throw exception when validating an invalid email address on a Spatie DTO object
I'm currently working on an integration with a third party API for an application, for which I'm using Spatie's data transfer object library. I'm currently looking to set up validation for some fields,...
1
vote
0
answers
207
views
Make modification to arguments before a method gets called
What would be the best way to perform some modification on a method argument before the method is actually executed?
I have tried achieving this with a combination of attributes and use of the ...
1
vote
0
answers
20
views
How to use PHP Attributes as Events? [duplicate]
Lets say i have an attribute class:
#[Attribute(Attribute::TARGET_METHOD)]
class MyEvent
{
/**
* @phpstan-param null|Request::METHOD_* $method
*/
public function __construct(
...
4
votes
1
answer
2k
views
PHPStan is not interpreting a Symfony EntityRepository as a generic
I'm having trouble making sense of this PHPStan error. PHPStan is saying I need to provide the class string of an EntityRepository of an object. I am providing the class string of a ...
0
votes
1
answer
4k
views
Attributes for Symfony route
In Symfony controllers, route are defined as follow :
class BlogController extends AbstractController
{
#[Route('/blog/{page}', name: 'blog_list', requirements: ['page' => '\d+'])]
public ...
4
votes
2
answers
5k
views
How to replace phpdocs with new attributes in php 8
I am using laravel. I need to know how phpdoc can be written in php 8 with attibutes.
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @...
12
votes
3
answers
4k
views
Symfony #[CurrentUser] attribute returns null
<?php
declare(strict_types=1);
namespace App\Controller\User;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
...
4
votes
1
answer
2k
views
PHP 8 Attribute constructor calling
I'm trying to learn how attributes in PHP work. But when I wrote some code (I have XAMPP with PHP 8 support installed), it doesn't seem to work (no message on the screen). Does it need additonal ...
1
vote
1
answer
1k
views
How to get method name from PHP 8 attribute (reflection)
I'm working on small PHP framework (for learning). It has a file with LoginController class containing a method with Route attribute (code below). Is there any way to get the name of the method in the ...
3
votes
2
answers
6k
views
Doctrine ORM 2.9 use both AnnotationDriver and AttributeDriver to parse entity metadata
Recently we upgraded our applications to PHP8.
Since PHP8 introduced attributes and doctrine/orm supports them as of version 2.9 it seemed like a good idea to utilize this feature to incrementally (ie....
1
vote
1
answer
1k
views
PHP 8 Attributes: Is it possible to validate metadata passed to attributes or force attribute instantiation?
Playing a little bit with the new PHP 8 attributes (annotations) (https://stitcher.io/blog/attributes-in-php-8) I started to create my own ones.
Simple example:
namespace CisTools\Attribute;
use ...
10
votes
1
answer
3k
views
How are PHP attributes different from doc block annotations?
Lately, the attributes RFC has passed the voting stage. How they are different from DocBlock annotations, and what benefits will they bring?
Consider simple Doctrine entity, before:
/**
* @ORM\Entity
...