Immutable objects in PHP?

后端 未结 6 1255
暗喜
暗喜 2020-12-31 06:34

Is it a good idea to create objects that cannot be changed in PHP?

For example a date object which has setter methods, but they will always return a new instance of

6条回答
  •  长发绾君心
    2020-12-31 07:11

    Immutable objects don't have setter methods. Period.

    Everyone will expect a setXyz() method to have a void return type (or return nothing in loosely typed languages). If you do add setter methods to your immutable object it will confuse the hell out of people and lead to ugly bugs.

提交回复
热议问题