User library issue, calling CI Library (session)

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

#2
[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');
  }
}

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.