-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
Schema: added possibility to instantiate object using reflection #47
Conversation
This reverts commit 9a02954.
Using reflection it is possible to instantiate object without constructor, which is pretty useful for constructor property promotion
} elseif ($this->castUsingReflection) { | ||
try { | ||
$reflection = new ReflectionClass($this->castTo); | ||
$instance = $reflection->newInstanceWithoutConstructor(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about sth like
new ($this->castTo)(...$value);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can probably have part of params promoted by constructor and part of params directly as class properties, reflection got them all.
38fdffd
to
343b9dd
Compare
ff661fd
to
aafca03
Compare
0df20de
to
36932a7
Compare
While using Nette/Schema I found little discomfort when using PHP 8 features - specifically Constructor property promotion.
Let's imagine following class:
Casting to this class will result in ArgumentCountError because Schema is instantiating the class using
new
keyword.This can be solved with instantiating the class using Reflection and it's
newInstanceWithoutConstructor
method.I solved this with adding second argument to castTo:
castTo(string $type, bool $usingReflection = false): self
- default behavior is not changed so it's not causing BC break.Tests included and passing.