schema icon indicating copy to clipboard operation
schema copied to clipboard

Schema: added possibility to instantiate object using reflection

Open davtk opened this issue 3 years ago • 0 comments

  • 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.

davtk avatar Jun 17 '22 10:06 davtk