Welcome Guest, Not a member yet? Register   Sign In
__call function equivalent.
#1

[eluser]pegas[/eluser]
Hy.
I won't to know how to create a function equivalent to __call() from php. I don't want to implement _remap function because i have like 20 or more functions each with different number of parameters(I'm trying to avoid writing the redirections for all the functions as to only redirect de incorrect url's)

I've tried to put the _call function inside my controller like so :
Code:
function __call($function,$args){
//i want to redirect anything that is not valid to the  index method for example
$this->index();
}
Can anyone help ?
#2

[eluser]mddd[/eluser]
__call is meant to handle requests to methods that do not exist in your class. However, in CI this doesn't happen (at least, not when calling a controller method) because CI checks if the method exists before trying to call it. If the method doesn't exist, CI will display an error, without trying. And so a __call method in your controller will never get called.

I think _remap is still your best option at the moment. I don't think it is so hard to make a _remap that simply checks if the called function is available. If so, you call it with all the arguments that were given. If not, you do your redirection to $this->index.
#3

[eluser]pegas[/eluser]
Nice explanation.
I know that if the method in the controller does not exist it redirects me to an error page that sais that the page doesn't exist. How can I modify that ?
#4

[eluser]mddd[/eluser]
The controller is fired up from /system/codeigniter/codeigniter.php, around line 231.
You could change that code to suit your needs.
But I would say that is not much easier than just writing a _remap that deals with your functions.
You dont't need to put every function by hand. Just write something smart to deal with all of them at once.
#5

[eluser]pegas[/eluser]
Cool. I get what you're saying. I was thinking of doing it like this it's that i was kinda fixed on the all "__call()" ideea.

Thanks for the quick responses.
#6

[eluser]Jelmer[/eluser]
Try something like this, using call_user_func_array():
Code:
function _remap()
{
    $params = $this->uri->segment_array();
    $function = array_shift($params);
    call_user_func_array(array($this, $function), $params);
}
I'd suggest adding some security measures (maybe only allow when it doesn't start with an underscore, like normal CI behavior), but basicly this should work.

Edit: I first posted it with the depricated call_user_method_array(), I just updated it to the non-depcrecated call_user_func_array().




Theme © iAndrew 2016 - Forum software by © MyBB