CodeIgniter Forums
Fatal error: Call to undefined function get_instance() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Fatal error: Call to undefined function get_instance() (/thread-4745.html)



Fatal error: Call to undefined function get_instance() - El Forum - 12-13-2007

[eluser]kelye[/eluser]
hello
i have the following problem...

Code:
MY_Input.php

class MY_Input extends CI_Input {
    var $CI;
    
    function My_Input()
    {
        parent::CI_Input();
        $this->CI =& get_instance();
    }
}

and i get a "Fatal error: Call to undefined function get_instance() ..."


Fatal error: Call to undefined function get_instance() - El Forum - 12-13-2007

[eluser]kelye[/eluser]
ok... found the problem

Input is a core class that loads and instantiates automatically BEFORE the Base4/5.php loads (where get_instance() is)

i think it would be nice to add a clear warning in docs about this


Fatal error: Call to undefined function get_instance() - El Forum - 12-13-2007

[eluser]tonanbarbarian[/eluser]
Yes there are a few things that are loaded before the controller
Once the controller is loaded a reference is made to the input class inside the controller so that you can access it there

If you need to extend the input and you need to access the controller in it here is what I suggest
still keep your CI property but do not instantiate it in the constructor

create a seperate method that can check for an instantiate the CI property and call there in any new code that you write where you might need to access the controller.
This way you dont break the input class and you only use the CI property when you needed it saving resources

Code:
class MY_Input extends CI_Input {
    var $CI = null;
    
    function My_Input()
    {
        parent::CI_Input();
    }

    function _inst() {
      if ($this->CI===null && function_exists('get_instance'))
        $this->CI =& get_instance();
    }

   function something() {
     $this->_inst();
     ...
   }
}



Fatal error: Call to undefined function get_instance() - El Forum - 12-13-2007

[eluser]kelye[/eluser]
thanks for the answer

the only problem with your code is that i have to call the _inst() function after the script is initialized... and then i will have $CI available

i've used some other functions instead of the resources needed from CI

thanks again