0

Basically in codeigniter, how can i use the system and application libraries within other libraries? So say I have a application library such as:

class User
{
   // some instance variables
}

Now lets say I want to use codeigniter session class in the User class, basically being able to work with sessions inside the User class. How can I go about doing this?

tereško
  • 58,060
  • 25
  • 98
  • 150
user1664427
  • 237
  • 4
  • 12

2 Answers2

2

The typical way to do this is to use get_instance(), which returns an instance of Codeigniter (actually the current controller class).

This will work from anywhere in your CI application:

get_instance()->session->set_userdata('key', 'value');
$segments = get_instance()->uri->segment_array();
// etc.

Typically in Codeigniter, you assign it to a class variable for ease of use:

class User
{
    private $CI;

    function __construct()
    {
        $this->CI = get_instance();
    }

    // Example
    function login()
    {
        $user = $this->get_user();
        $this->CI->session_set_userdata('user_id', $user->id);
    }
}

This does create a lot of dependency on the state of CI and the different loaded classes, rather than passing instances of them in directly to the User class via Dependency Injection, but this is the typical flow for someone developing a CI library.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • Don't forget to assign by reference with: `=&` ;) – Jordan Arsenault Oct 15 '12 at 20:53
  • @JordanArseno: I was under the impression that was no longer necessary these days as all objects are assigned by reference (think it was in PHP5) unless explicitly `clone`d, for example: http://codepad.org/fOSgTiP4. However it's probably still good practice. Let me know if I'm mistaken, I'd like to know. – Wesley Murch Oct 15 '12 at 21:02
  • You very well could be correct, Wesley. I'm just taking the CI docs on faith and haven't looked into it any further than that! – Jordan Arsenault Oct 16 '12 at 18:05
  • Probably good practice anyways, but I believe CI has at last dropped PHP4 support. Honestly I haven't kept up with CI much these days, and am drifting farther and farther away from it although I still use it on a daily basis. – Wesley Murch Oct 16 '12 at 18:10
0

Ideally you would just pass in the session data you needed from your model or controller the loads the library.

Otherwise, this should work from any file:

    <?php
    ob_start();
    include('index.php');
    ob_end_clean();
    $CI =& get_instance();
    $CI->load->library('session'); //if it's not autoloaded in your CI setup
    echo $CI->session->userdata('name');      
    ?>

I found this answer on a similar question.

Community
  • 1
  • 1
Justin
  • 26,443
  • 16
  • 111
  • 128
  • I think we answered different questions, I guess the OP can clarify what they meant. I've had this problem as well too, specifically needing CI session data in third party "plugins" that are outside the codeigniter app completely. – Wesley Murch Oct 15 '12 at 20:26