Is it possible to specify more than one type hint for a parameter?

joao picture joao · Oct 1, 2010 · Viewed 14.7k times · Source

Is there a way to add more than one type hinting to a method? For example, foo(param) must receive a instance of string OR bar OR baz.

Answer

PatrikAkerstrand picture PatrikAkerstrand · Oct 1, 2010

That is not possible to enforce (except inside the method). You can only provide a single type hint, and only to objects/interfaces and arrays (since PHP 5.1).

You can/should however document it in your method, i.e:

/**
 * @param string|Bar|Baz $param1
 */
function foo($param1);