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

跟風遠走 提交于 2019-12-24 10:23:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!