问题
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).
回答1:
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));
回答2:
Generally speaking you cant use ArrayObject
or and ArrayAccess
implementor with native functions that operate on arrays. You can use them with language constructs that typically use array notation for traversing and what not.
来源:https://stackoverflow.com/questions/13140657/arrayaccess-arrayobject-do-not-work-with-functions-like-call-user-func-array