Is there any way to typehint (via phpdoc) an ArrayAccess class that only contains a type?
So if I have "class AnimalCollection implements ArrayAccess {}" where every item is an instanceof Animal is there any way to typehint it so functions expand when I type, say, "$animalCollectionInstance[0]->respirate()"?
Please sign in to leave a comment.
Hi there,
You can easily typehint the variable/property. I do not think that it can be done for the class itself (nothing comes into my my right now).
If you need such variable to have methods from both Collection and Array ... then combine them there:
What would probably work is to use type declarations in the method signatures that implement the \ArrayAccess interface. You should try this for at least offsetGet() and offsetSet(). I'm not sure if that would mean it incorrectly implements the interface or not, or perhaps that could be considered to be the case by some QA tool you use, but it is worth a shot. It would look like this:
public function offsetGet(
$offset): \Animal { ... }public function offsetSet($offset, \Animal $value): void { ... }