-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
[Feature] Allow "write once" initialization of fields #4
Comments
By Nikita:
|
By the way, this is a common pattern for immutable objects public function withRed(int $red): self
{
assert($red >= 0 && $red <= 255);
$color = clone $this;
$color->red = $red;
return $color;
} Does this enhancement can may work it at least with public function withRed(int $red): self
{
assert($red >= 0 && $red <= 255);
$color = clone $this;
unset($color->red);
$color->red = $red;
return $color;
} ?
|
I also have this case :) |
I’m surprised that you have these cases 😃 Currently only ctor and static methods are allowed, sorry. But your comment was like small inspiration )) PS. Not sure, if I’ll be able to return to my projects soon. Very busy on my new work... |
@lisachenko no problem, I would like to have more time as well. So, I could contribute to the package. I think what you did is a really good work. Just to explain a bit about my case: we need to keep the historical change log of an entity and there are many actions that have to be performed upon a version of the entity. The way we are doing it is by making the entity immutable and, everytime you need to update a field, we have this We could of course use a contructor for it, but cloning and chaging makes the implementation a bit cleaner. I see your point of restricting mutability only within ctor and static methods and I actually agree with it, but I was wondering if there could be a solution that would be useful in our case. |
Logic of typed properties requires initialization and engine allows to initialize such properties from everywhere, not only within constructor.
@nikic has suggested to follow this logic, eg. allow to initialize each property only once. Such implementation will allow to call private/protected setters with additional logic of initialization/validation.
The text was updated successfully, but these errors were encountered: