0

I have a complicated scenario based on an existing framework I'm using which is forcing me to deal with nested call_user_func_array calls. I've got a file with two functions:

function their_caller($options)
{
  call_user_func_array('their_callback', $options);
}

function their_callback($options)
{
  call_user_func_array($options[0], $options[1]);
}

Then I have another file with a class with and some public methods:

namespace FOO;

class MyClass
{
   public function my_caller()
   {
     $operations = array(
       array(
         array($this, 'my_callback'),
         'Hello World'
       )
     );

     their_caller($operations);
   }

   public function my_callback($text)
   {
     print $text;
   }
} 

When I call my_caller() on a new instance of MyClass, it calls their_caller which then passes an array of arguments containing a reference to MyClass (as $this) as well as the method my_callback.

their_caller() then forwards the request to their_callback and passes $options along.

In their_callback I can debug $options[0] and see that it's an array containing a reference to MyClass and my_callback.

I call get_class_methods() on $options[0][0] in their_callback and it will show the list of methods, but for some reason, call_user_func_array($options[0], $options[1]); won't call my_callback on MyClass. I can even call $options[0][0]->my_callback('HELLO'); and it works.

Unfortunately, I can't modify their_caller or their_callback. They are part of the framework. Any ideas what's preventing it from working?

Ward
  • 3,318
  • 3
  • 30
  • 50
  • oddly enough, `$options[0][0]->$options[0][1]($options[1]);` works from within `their_callback`. – Ward May 31 '17 at 14:01

1 Answers1

0

Note: Your function their_callback will receive two arguments.

Try this code snippet here

<?php
ini_set('display_errors', 1);
function their_caller($options)
{
    call_user_func_array('their_callback', $options);
}
//first argument will receive callback
//second argument will receive the text which you want to send as argument.
function their_callback($callback,$option) 
{
    //call_user_func_array should expect second argument as array not string.
    call_user_func_array($callback, array($option));
    //here we have converted `$option` string(Hello World) to array.
}

class MyClass
{
    public function my_caller()
    {
        $operations = array(
        array(
         array($this, 'my_callback'),
                    'Hello World'
                )
            );
        call_user_func_array('their_caller', $operations);
    }

    public function my_callback($text)
    {
        print $text;
    }


} 
$object= new MyClass();
$object->my_caller();
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • The original parameter passed to `their_caller` is an array of arrays so `their_callback` only receives one parameter (the interior array) which at index 0 is an array with a reference to `MyClass` and `'my_callback'` and at index 1 is the string that I am trying to pass through. – Ward May 31 '17 at 12:49
  • @Ward The above which you are trying can be achieved in this way also. https://eval.in/808757 here i have changed no of parameters of `their_callback` and changed your `$operations` array, you can try debugging of my code https://eval.in/808668 – Sahil Gulati May 31 '17 at 14:46