Search code examples
phpmagic-methodsarrayaccess

ArrayAccess/ArrayObject do not work with functions like call_user_func_array()


When implementing an object using ArrayAccess or ArrayObject, to some operations it's a perfectly normal array (for instance a foreach() statement). Others, however, are not so easily fooled and still complain it is an object:

[E_WARNING] call_user_func_array() expects parameter 2 to be array, object given

This strikes me as incosistent. Can someone explain the reasoning behind this? Is there a way around this?

I need this to support backend code. It requires an array (passed as a parameter to call_user_func_array()), and sometimes modifies it. I need to mirror any changes made to the array to the new variables however, so that's why I tried to do it via an ArrayAccess object (more info here).


Solution

  • Thye function name is pretty explicit, and the description in the documents indicates why it requires an array

    The solution is to wrap your object inside an array

    call_user_func_array('callback', array( $myObject));