Schema: added possibility to instantiate object using reflection#47
Closed
vitek-dev wants to merge 15 commits intonette:masterfrom
vitek-dev:master
Closed
Schema: added possibility to instantiate object using reflection#47vitek-dev wants to merge 15 commits intonette:masterfrom vitek-dev:master
vitek-dev wants to merge 15 commits intonette:masterfrom
vitek-dev:master
Conversation
This reverts commit 9a02954.
Using reflection it is possible to instantiate object without constructor, which is pretty useful for constructor property promotion
JanTvrdik
reviewed
Jun 20, 2022
| } elseif ($this->castUsingReflection) { | ||
| try { | ||
| $reflection = new ReflectionClass($this->castTo); | ||
| $instance = $reflection->newInstanceWithoutConstructor(); |
Contributor
There was a problem hiding this comment.
How about sth like
new ($this->castTo)(...$value);
Author
There was a problem hiding this comment.
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
dg
added a commit
that referenced
this pull request
Feb 5, 2023
dg
added a commit
that referenced
this pull request
Jul 30, 2023
dg
added a commit
that referenced
this pull request
Aug 5, 2023
dg
added a commit
that referenced
this pull request
Sep 26, 2023
dg
added a commit
that referenced
this pull request
Sep 26, 2023
dg
added a commit
that referenced
this pull request
Oct 2, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
newkeyword.This can be solved with instantiating the class using Reflection and it's
newInstanceWithoutConstructormethod.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.