Can I create a function inside a controller dinamically from a model?

#1
[eluser]chefnelone[/eluser]
hello,

Can I create a function inside a controller dinamically from a model? Is this possible??

let's say I have this controller with just the function first():

index_controller.php
Code:
class Index extends MY_Controller {
   function first(){
      //do something
   }

}

What I need is create another function inside the Index class, let's say: second() without open the file and writting it but from a index_model.php

index_model.php
Code:
class Index extends MY_Model {
   function create_functions(){
      //CREATE second() FUNCTION INSIDE index_controller.php
   }

}

index_controller.php
Code:
class Index extends MY_Controller {
   function first(){
      //DO SOMETHING
   }
   function second(){
      //THIS WAS CREATED FROM THE MODEL
   }

}

#2
[eluser]WanWizard[/eluser]
No, but you can use the magic function __call() in your controller to capture calls to undefined methods:
Code:
function __call($method, $params)
{
    if ($method == 'second')
    {
        // do something here
    }
    else
    {
        return NULL;
    }
}

Still not very dynamic, but it's the only option.

I fail to see why you would want this though, where are you going to call this controller method()? If it's name is dynamic, that's going to be a challenge. If not, why not code it in the first place? You have to code it somewhere...

#3
[eluser]chefnelone[/eluser]
hi wan.
I'm creating pages dinamically for a site, each one of this pages needs a function. I known that even if I get the function created then after I'll need to do some code for the function, but this save some time and avoid errors messages.

I'll take a look a the __call() function. I didn't know it and seems very useful.

#4
[eluser]jiffier[/eluser]
The model shouldn't have access to the controller, otherwise you're violating MVC and the separation of concerns.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.