-
-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use non-nullable properties with AccessInterceptorScopeLocalizer #682
Comments
Yes, that is expected, since proxymanager needs a default value to exist
for these properties.
…On Tue, Apr 6, 2021, 19:33 Petr Buchin ***@***.***> wrote:
For now it's impossible to create proxies for objects that have
non-nullable properties, even if that properties are being set in
constructor.
Example:
namespace Test;
use ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory as ProxyFactory;
class Foo
{
public function __construct(protected Bar $bar)
{
}
}
$factory = new ProxyFactory();$foo = new Foo();
$factory->makeProxy($foo, [], []);
This code generates an exception:
[ProxyManager\Exception\UnsupportedProxiedClassException]
Cannot create references for following properties of class Test\Foo: bar
Is this an expected behavior?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#682>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABFVEGZSDVMUR7ZERDAL63THNAU7ANCNFSM42PFPIAA>
.
|
@Ocramius as far as I see, this behavior can be fixed, if method This way it can be sure that this properties will be initialized at the time of referencing them. Do I understand that right? If so, I could create a PR to address this issue. |
ProxyManager cannot arbitrarily assign a value to those properties, and
they cannot be unset, unless a default value can be assigned by the tool.
Hence you need to modify your ctor to be `__construct(private ?A $a =
null)`, or declare the property explicitly, like we did pre-php-8.0.
Note that PHP 8.0 constructor signatures are automatically promoted to
property declarations: inspecting the constructor has no effect.
…On Tue, Apr 6, 2021, 19:42 Petr Buchin ***@***.***> wrote:
@Ocramius <https://github.com/Ocramius> as far as I see, this behavior
can be fixed, if method Properties::onlyNonReferenceableProperties() will
use constructor method reflection to see, what arguments does constructor
have.
This way it can be sure that this properties will be initialized at the
time of referencing them.
Do I understand that right?
If so, I could create a PR to address this issue.
|
@Ocramius I tried declaring property explicitly and having standard constructor with assigning this property a value, but that did not help |
Sorry, I wasn't clear enough: the property must be nullable **and** have a
default value of `null` declared:
```php
private ?A $a = null;
```
…On Tue, Apr 6, 2021, 20:14 Petr Buchin ***@***.***> wrote:
or declare the property explicitly, like we did pre-php-8.0.
@Ocramius <https://github.com/Ocramius> I tried declaring property
explicitly and having standard constructor with assigning this property a
value, but that did not help
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#682 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABFVEHRTVO25WLIJM67PKTTHNFRFANCNFSM42PFPIAA>
.
|
@Ocramius oh, that's too bad to declare all properties as nullable :( But anyway, thank you very much for your help! |
Yeah, it's a limitation that has been in place since PHP 7.4.
Note that decorator proxies still operate well with that.
…On Tue, Apr 6, 2021, 20:18 Petr Buchin ***@***.***> wrote:
@Ocramius <https://github.com/Ocramius> oh, that's too bad to declare all
properties as nullable :(
But anyway, thank you very much for your help!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#682 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABFVEATGJ5N6USYIGGAHG3THNF7XANCNFSM42PFPIAA>
.
|
@Ocramius unfortunately I need to use this kind of proxies, because I need to intercept method calls on $this, and they are not intercepted by value holder proxy. What do you think about creating a proxy type that will be similar to scope localizer, but will use Closure::call() to access properties instead of direct references? It probably would be slower, but it will have it’s use cases. I would be happy to implement that kind of proxy in this library instead of creating another one. |
I'm not aware of a way to make property access transparent, without
unsetting properties: if you have an idea/prototype on how to re-implement
this kind of proxy, feel free to propose a prototype, but please do test it
against the integration tests 😅
…On Tue, Apr 6, 2021, 20:33 petr-buchyn ***@***.***> wrote:
@Ocramius <https://github.com/Ocramius> unfortunately I need to use this
kind of proxies, because I need to intercept method calls on $this, and
they are not intercepted by value holder proxy.
What do you think about creating a proxy type that will be similar to
scope localizer, but will use Closure::call() to access properties instead
of direct references?
It probably would be slower, but it will have it’s use cases.
I would be happy to implement that kind of proxy in this library instead
of creating another one.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#682 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABFVEBMIH2NVFDOOOK2KTTTHNHWLANCNFSM42PFPIAA>
.
|
For now it's impossible to create proxies for objects that have non-nullable properties, even if that properties are being set in constructor.
Example:
This code generates an exception:
Is this an expected behavior?
The text was updated successfully, but these errors were encountered: