N
N
Nurlan2017-05-18 22:07:45
PHP
Nurlan, 2017-05-18 22:07:45

Why are immutable objects needed?

Hello.
What for is necessary, for example, DateTimeImmutable? A class in which I can drive the date, and using add or sub nothing will happen to it. I'm wondering, rather than how to use them, but situations in which they may be needed.
So that the code could not do anything with the date, if you really want to? And why the code that tries to do this with a date with which this should not be done. I found a lot of enthusiastic comments, but I didn’t find the words “why” themselves.
I really hope for your help. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kstyle, 2017-05-18
@kstyle

https://habrahabr.ru/company/mailru/blog/301004/

E
Eugene, 2017-05-18
@immaculate

You should read articles about functional programming and functional programming languages ​​(like Lisp). Immutability reduces the number of errors in the code, since the state of an object cannot be suddenly changed, for example, in another thread, or another function call. You always know what you are working with.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question