Search code examples
phpcodeignitercallbackclass-methodcall-user-func-array

How to write a specific model method as the callback in call_user_func_array()?


I'm been trying to send a dynamic number of arguments to a function in Codeigniter and have been running into a wall with call_user_func_array.

One error I'm getting "Undefined property: Account::$callShowMessage" is making me wonder if it's even possible to refer to a function in another file with call_user_func_array/CodeIgniter.

If anyone could help point me into the right direction, it would be greatly appreciated.

Array:

Array ( [0] => Current password is incorrect [1] => New passwords don't match )

Controller Excerpt:

$this->session->set_flashdata('msg', call_user_func_array($this->generic_model->callShowMessage, $msg));

Function:

public function callShowMessage()
{   

$args = func_get_args();  

foreach ($args as $key => $value) {  
    $msg .= "$value<br />";  
}   
$msg = addslashes($msg);
return "<script type='text/javascript'>$(document).ready(function() { showMessage('$msg') });</script>";
}

Solution

  • call_user_func_array(array($this->generic_model, 'callShowMessage'), $msg)
    

    To pass a method of an object as callback, use the above array syntax with the object and the name of the method. See http://php.net/manual/en/language.pseudo-types.php#language.types.callback(dead link).