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

StackOverflow https://stackoverflow.com/questions/13140657

  •  21-07-2021
  •  | 
  •  

Pergunta

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).

Foi útil?

Solução

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));

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top