PHP function, check parameter data types and valid values?
Posted: Mon Oct 29, 2012 6:36 pm
When writing functions in PHP how extensively should one check and validating the data in parameters?
For example if I expect a parameter to be a boolean, should I cast it as such?
Or check that the parameter type is boolean and if not return FALSE?
I don't particularly like the idea of returning FALSE from a function just because the parameter's invalid, unless the function of the function (urrg) is to validate some data. Because it can mask the actual result of the function if it is designed to return TRUE/FALSE anyway.
If the function generates a PHP error resulting from invalid data being supplied then in a way that's more useful to the developer than just returning FALSE or returning an empty string. That way they know their input data (and therefore possible usage of the function) is invalid/incorrect rather than the tests within the function just returning FALSE rather than TRUE.
I could always check the data coming in to the function and trigger_error() if the data is not the type that's expected. That might make a function act a bit more like PHP's internal functions when you provide invalid params. But to what extent is this necessary?
I'm aware of the loosely typed nature of PHP. In fact that's what makes it such an easy language to use. But I'm wondering if I should be testing the values and types of parameters when they come in to the function. At the moment I tend to just rely on the developer (me) being sensible and realising if there's an error with that function they need to make sure they're using it correctly. It's only ever me that uses these functions at the moment but what if I released code for others to use?
Cheers, B
For example if I expect a parameter to be a boolean, should I cast it as such?
Or check that the parameter type is boolean and if not return FALSE?
I don't particularly like the idea of returning FALSE from a function just because the parameter's invalid, unless the function of the function (urrg) is to validate some data. Because it can mask the actual result of the function if it is designed to return TRUE/FALSE anyway.
If the function generates a PHP error resulting from invalid data being supplied then in a way that's more useful to the developer than just returning FALSE or returning an empty string. That way they know their input data (and therefore possible usage of the function) is invalid/incorrect rather than the tests within the function just returning FALSE rather than TRUE.
I could always check the data coming in to the function and trigger_error() if the data is not the type that's expected. That might make a function act a bit more like PHP's internal functions when you provide invalid params. But to what extent is this necessary?
I'm aware of the loosely typed nature of PHP. In fact that's what makes it such an easy language to use. But I'm wondering if I should be testing the values and types of parameters when they come in to the function. At the moment I tend to just rely on the developer (me) being sensible and realising if there's an error with that function they need to make sure they're using it correctly. It's only ever me that uses these functions at the moment but what if I released code for others to use?
Cheers, B