I need to create an immutable class which is simply a member field container. I want its fields to be instantiated once in its constructor (the values should be given as parameters to the constructor). I want the fields to be public but immutable. I could have done it with Java using the final keyword before each field. How is it done in PHP?
I need to create an immutable class which is simply a member field container.
Share
You should use
__setand__getmagic methods and declare that property as protected or private:Example:
Outputs:
@property-readshould help your IDE acknowledge existence of this magic property.