0

I have the following code I added to a MY_Controller that is being extended by all my controllers:

public function _remap($method, $params = array())
    {//exit($this->router->fetch_class());
        if (array_search($method, $this->private_methods) !== false && !$this->logged_in)
        {
            $this->session->set_flashdata('message', array(
                                                        'message'   =>  'You must login to access the requested area',
                                                        'error'     =>  1
                                                        )
                                        );
            redirect('/');
        }
        else if (method_exists($this, $method))
        {
            $this->$method($params);
        }
        else
        {
            redirect('/');
        }

    }

The issue being created is that the call $this->$method($params) is condensing the params in to an Array. So a method such as the following breaks:

function some_method($param1, $param2, $param3)

Is there a way to break this array back into individual items for functions like this?

Lior Elrom
  • 19,660
  • 16
  • 80
  • 92
Lee Loftiss
  • 3,035
  • 7
  • 45
  • 73
  • That was my post, and the code above is based on that code. The code in the link you are referring to does not separate the parameters, it sends them as an ARRAY. – Lee Loftiss Feb 28 '13 at 03:51
  • You might doing something wrong. I tested and it is sending individual parameters. Create `Example` controller and test the answer. – Madan Sapkota Feb 28 '13 at 04:10

2 Answers2

1

I was trying to do the same and find the same problem with

$this->$method($params);

I found another option

call_user_method_array($method,$this,$params);

Which worked but is already deprecated. Function in PHP deprecated, what should I use now?

But hopefully that is the new one.

call_user_func_array(array($this,$method), $params);
Community
  • 1
  • 1
abordonado
  • 11
  • 3
1

I have tried this and it worked for me

public function _remap($method,$params = array())
  {
    if (method_exists($this, $method)) {

      if($method == 'delete_photo' || $method == 'delete_video'){

        call_user_func_array(array($this,$method), $params);
        }

      else{
        $this->$method();
        }
      }
      else {
      $this->index($method);
    }
  }

Now you just have to replace "delete_photo" & "delete_video" with your methods names that contains parameters, and of course you can add as much methods as you want.

matlab30
  • 11
  • 1