Is there a way to require a function parameter is a specific datatype such as an array? For instance something like this:
function example(array $array){}
If so does that work for all datatypes? Is there a good resource that would show me how to do this?
Edit: Yes, you can type-hint with arrays, so edited my answer and changed accordingly.
What you want to do is called type-hinting. You can’t type hint basic data types, such as
int,string,bool. You can type-hint witharrayor objects and interfaces:Calling
example_hinted1(5)will generate a PHP fatal error (not an exception), but calling it passing an array is totally ok.If you need to be sure that some argument to a function is from a basic type you can simulate this behavior with code inside your function:
So, these snippets would work:
while these would throw an exception: