CI Codeigniter Fatal error: Call to a member function userdata() on a non-object
Codeigniter provides in helper to defined $CI and used in functions helper, in some versions of codeigniter it cannot load on defined function in functions helper, in older versions it we just load in variable called $CI and used in functions helper $CI instead $this, that this we using in codeigniter model and controllers, here is I am sharing three types of solution to $CI.
first code this works fine in older versions of codeigniter.
Note: you must load related libraries and helpers if you didn’t auto loaded them from config folder autoloads.php
//GET INSTANCE $CI = &get_instance(); $CI->load->database(); $CI->load->helper('url'); function get_userdata($sessionname){ return $CI->session->userdata($sessionname); }
In some of old versions of codeigniter above code cannot work
To fix this issue just make global variable of $CI in inside of function.
//GET INSTANCE $CI = &get_instance(); $CI->load->database(); $CI->load->helper('url'); function get_userdata($sessionname){ global $CI; return $CI->session->userdata($sessionname); }
Newer Codeigniter versions like 2.2.0 they also thrown error even I globalize variable of $CI in inside of function.
CI Codeigniter Fatal error: Call to a member function userdata() on a non-object
I have also faced this issue and I fixed it like bellow code, I just create a function called ci_initialize
function ci_initialize(){ $CI = &get_instance(); $CI->load->database(); return $CI->load->helper('url'); } function get_userdata($sessionname){ $CI = ci_initialize(); return $CI->session->userdata($sessionname); }