I would like to know if I can use Partial
like typescript.
For instance I have this class:
class Registration {
public function __construct(
public readonly int $year,
public readonly int $coverage,
public readonly string $start_date,
public readonly string $state_address,
) {
}
}
Is there a way to create a class from the original (without modifying it) but make it "Partial". Where all attributes are optional or null. Something like this:
class RegitrationPartial extends Partial(Registration) { }
Now I can call that class in this way:
new RegitrationPartial(year: 2025);
2
Answers
You can use something called named arguments, this allows you to specify the exact argument you want to populate. Your actually pretty close already.
Some further reading on named arguments
https://stitcher.io/blog/php-8-named-arguments
You can create a RegistrationPartial class that extends the original class and uses optional parameters in its constructor, providing null as default values. after that, you can send those optional parameters to the parent class based on whether they are provided or not.
this one should work: