Welcome Guest, Not a member yet? Register   Sign In
Beginner's Question: Can I instantiate a controller from within a controller to access member functions?
#1

[eluser]Unknown[/eluser]
Hey CI,

Basically I was wondering if it would be better to create a helper for this functionality or dig to find out if the following is possible:

Code:
class Index extends Controller{

       var $persist = array();

       function __construct()
       {
              parent::Controller();
              $persist['item'] = $this->mycontroller->myfunction();
       }

Thanks for your help
#2

[eluser]richzilla[/eluser]
Short answer - no.

The simplest way around your problem as far as i can see, is to use a MY_Controller. This way you can store functions that may be used in many controllers all in one place. to do this create a MY_Controller.php file in the libraries folder of your application.

Code:
<?php

    class MY_Controller extends Controller{
        
        function MY_Controller()
        {
            parent::Controller();    
        }
        
        
        
    }

Then have all of your controllers inherit this class:

Code:
<?php

    class Controller_name extends MY_Controller{

        function __construct()
        {
             parent::MY_Controller();
        }

    }

This way any functions in your MY_controller will also be inherited by the controller.


Hope this helps.




Theme © iAndrew 2016 - Forum software by © MyBB