CodeIgniter Forums
User library issue, calling CI Library (session) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: User library issue, calling CI Library (session) (/showthread.php?tid=6371)



User library issue, calling CI Library (session) - El Forum - 02-24-2008

[eluser]skrobma[/eluser]
I created my own library and I'm trying to access information in my session, but I getting the following error. What am I doing wrong? (PHP5/MYSQL5(WAMP5:1.7.3), CI:1.6.1)):

Code:
<p>Severity: Notice</p>
<p>Message:  Undefined variable: CI</p>
<p>Filename: libraries/Podclass.php</p>
<p>Line Number: 20</p>

Podclass.php
Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Podclass {
    
    /**
     * Constructor
     *
     */    
    function Podclass()
    {    
        $CI =& get_instance();
         $CI->load->library('session');
    }
    
    function lerror()
    {
        $errors = array(0=>' ',1=>
            "<DIV class='error'> Invalid UserID/Password </DIV>",2=>
            "<DIV class='error'> You don't have access to this pod.</DIV>");
            return $errors[$CI->session->userdata('lerror')];
        
    }

}
// END podclass Class
?&gt;

Thanks, in advance.


User library issue, calling CI Library (session) - El Forum - 02-24-2008

[eluser]Michael Wales[/eluser]
Scope. Your variable $CI is only available to the Podclass() function. Make it a class variable, like so:

Code:
class Podclass {
  var $CI;

  function Podclass() {
    $this->CI =& get_instance();
    $this->CI->load->library('session');
  }

  function lerror() {
    return $this->CI->session->userdata('lerror');
  }
}



User library issue, calling CI Library (session) - El Forum - 02-24-2008

[eluser]skrobma[/eluser]
Thanks, that worked!!