Skip to content
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

Closed
wants to merge 15 commits into from
Closed

Schema: added possibility to instantiate object using reflection #47

wants to merge 15 commits into from

Conversation

vitek-dev
Copy link

  • bug fix / new feature: new feature
  • BC break? no
  • doc PR: nette/docs#??? will write if accepted

While using Nette/Schema I found little discomfort when using PHP 8 features - specifically Constructor property promotion.

Let's imagine following class:

class Foo
{
  public function __construct(
    public int $a,
    public int $b,
  ) {
  }
}

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.

} elseif ($this->castUsingReflection) {
try {
$reflection = new ReflectionClass($this->castTo);
$instance = $reflection->newInstanceWithoutConstructor();
Copy link
Contributor

@JanTvrdik JanTvrdik Jun 20, 2022

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);

Copy link
Author

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.

dg added a commit that referenced this pull request Oct 3, 2023
dg added a commit that referenced this pull request Oct 3, 2023
dg added a commit that referenced this pull request Oct 3, 2023
dg added a commit that referenced this pull request Oct 4, 2023
dg added a commit that referenced this pull request Oct 4, 2023
dg added a commit that referenced this pull request Oct 4, 2023
dg added a commit that referenced this pull request Oct 5, 2023
dg added a commit that referenced this pull request Oct 5, 2023
dg added a commit that referenced this pull request Oct 5, 2023
dg added a commit that referenced this pull request Oct 5, 2023
dg added a commit that referenced this pull request Oct 5, 2023
dg added a commit that referenced this pull request Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants