Is it possible to allow an Array or an object that implements ArrayAccess?
For example:
class Config implements ArrayAccess {
...
}
class I_Use_A_Config
{
public function __construct(Array $test)
...
}
I want to be able to pass in either an Array or ArrayAccess.
Is there a clean way to do this other than manually checking the parameter type?
No, there is no “clean” way of doing it.
The
arraytype is a primitive type. Objects that implement theArrayAccessinterface are based on classes, also known as a composite type. There is no type-hint that encompasses both.Since you are using the
ArrayAccessas an array you could just cast it. For example:If that is not an option (you want to use the
Configobject as it is) then just remove the type-hint and check that it is either an array or anArrayAccess. I know you wanted to avoid that but it is not a big deal. It is just a few lines and, when all is said and done, inconsequential.