Castables don't work when used from a constructor #395
-
Hi everyone, I am using the latest 3.2.1 version of the package with Laravel 10 and PHP 8.2. As an example, I used the sample provided in the documentation: class ForgotPasswordRequest extends Data
public function __construct(
#[WithCastable(Email::class)]
public Email $email,
) {}
} class Email implements Castable
{
public function __construct(public string $email)
{
}
public static function dataCastUsing(...$arguments)
{
return new class implements Cast
{
public function cast(DataProperty $property, mixed $value, array $context): Email
{
return new Email($value);
}
};
}
} When I try to create the request via the constructor, I get a type error message: $forgotPasswordRequest = new ForgotPasswordRequest('[email protected]');
However, it works as expected when using the $forgotPasswordRequest = ForgotPasswordRequest::from(['email' => '[email protected]']); Since the documentation is not explicit enough about that part, is that supposed to be an issue or the intended behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This is expected behavior, we cannot overwrite this stuff since it is not possible in PHP |
Beta Was this translation helpful? Give feedback.
-
Hi @rubenvanassche, glad it got confirmed then. Thank you for your answer! |
Beta Was this translation helpful? Give feedback.
This is expected behavior, we cannot overwrite this stuff since it is not possible in PHP