Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Sebastian de Deyne:
Normalize Your Values on Input
Mar 11, 2016 @ 11:55:58

In a post to his site Sebastian de Deyne makes the suggestion that you should normalize your values (input) as soon as possible.

Dynamic languages allow us to pass anything as a parameter without requiring a specific type. In turn, this means we often need to handle some extra validation for the data that comes in to our objects.

This is a lightweight post on handling your incoming values effectively by normalizing them as soon as possible. It's a simple guideline worth keeping in mind which will help you keep your code easier to reason about.

He gives an example of a HtmlClass object instance that can take in either a single string or an array of strings. With this structure he shows the complexity it would add for methods like toArray and toString. Instead he recommends normalizing the value in the constructor, making it an array if it's not already. The the code required in the rest of the class to use/translate it is much simpler.

tagged: normalize values input array string example tutorial

Link: https://sebastiandedeyne.com/posts/2016/normalize-your-values-on-input