CodeIgniter Forums
Beginner's Question: Can I instantiate a controller from within a controller to access member functions? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: Beginner's Question: Can I instantiate a controller from within a controller to access member functions? (/thread-26369.html)



Beginner's Question: Can I instantiate a controller from within a controller to access member functions? - El Forum - 01-12-2010

[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


Beginner's Question: Can I instantiate a controller from within a controller to access member functions? - El Forum - 01-12-2010

[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.