How do you validate/manage your PHP method arguments and why do you do it this way?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, assuming that you’re talking about type-checking method arguments, it depends:
If it’s expecting an object, I use type-hinting with an interface:
If it’s expecting an array only, I use type-hinting with the
arraykeyword.If it’s expecting a string, int, bool or float, I cast it:
If it’s expecting mixed, I just check in a cascade:
Lastly, if it’s expecting an iterable type, I don’t use type-hinting. I check if it’s an array first, then re-load the iterator:
If it’s expecting a filename or directory, just confirm it with
is_file:I think that handles most of the cases. If you think of any others, I’ll gladly try to answer them…