I have a class that uses the __set magic method. One of the properties for the class can only be set with a certain range of string values, the best example I can think of is the mysql datatype ENUM(‘value_one’,’value_two’,’value_three’).
Would I place conditional statements within the __set method to distinguish between which property is being set and whether the value is valid for that property?
Are large switch statements inside a __set method considered sloppy practice, is their a better way of producing the desired results?
Thanks,
Ben
Yes to the first part, no to the second.
Your
__setmethod should determine which variable is trying to be set and then pass off the rest of the functionality to a private settersetParticularVar.setParticularVarwill then contain the logic specific to validating that variable and can throw an exception if an invalid value is passed.Alternatively, you could make the private setter public instead, allowing two ways to set the variable but this is not as desirable as two ways to set a value is not as clean a design.