Re: Attributes and strict types
| From: | Brent Roose | Date: | Tue, 22 Sep 2020 13:17:18 +0000 |
| Subject: | Re: Attributes and strict types | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111917@lists.php.net to get a copy of this message | ||
Hi Nikita
From a userland point of view, I'd indeed expect this example to use strict_types, because
it's declared in UseOfMyAttribute.php. Is this something that can be changed still?
Kind regards
Brent
> On 22 Sep 2020, at 14:58, Nikita Popov <nikita.ppv@gmail.com> wrote:
>
> Hi internals,
>
> I would like to clarify how attributes are supposed to interact with
> strict_types. Consider the following code, split up into three files to
> make it clear that each one could have their own strict_types mode:
>
> MyAttribute.php
> <?php
> #[Attribute]
> class MyAttribute {
> public function __construct(string $a) {}
> }
>
> UseOfMyAttribute.php
> <?php
> declare(strict_types=1);
> #[MyAttribute(42)]
> class Test {}
>
> AccessOfAttribute.php
> <?php
> var_dump((new
> ReflectionClass(Test::class'))->getAttributes()[0]->newInstance());
>
> Currently, what happens is that this code will construct the attribute,
> converting int 42 into string "42", even though UseOfMyAttribute.php has
> strict_types=1.
>
> My intuitive expectation here would be that we should be following the
> strict_types mode of wherever the attribute is being used (i.e.
> UseOfMyAttribute.php, not MyAttribute.php or AccessOfAttribute.php).
> Currently, we always assume strict_types=0.
>
> Is my expectation correct?
>
> Regards,
> Nikita