Eddie Banz
Eddie Banz

Reputation: 49

$CI is not available in function php

not sure if this is already answered, but i cant seem to find the right answer to my question

I am creating a library, and my library is using an existing model. i am having trouble using the $CI outside the __construct.

here is a snippet of my code.

class something-todo {

protected $CI;

public function __construct(){
    $this->$CI =& get_instance();
    $CI->load->model('Sites');  
}

public function get_records()
{
    $results = $CI->Sites->get_activeRecords();
 // other lines of code
 }

whenever i am calling this library, I am getting a $CI error inside the get_records() function.

not sure what else to do.

thank if this is a duplicate, thank for the redirect. otherwise, thank in advance for any answer.

Upvotes: 0

Views: 42

Answers (3)

codex
codex

Reputation: 446

class something-todo {

protected $CI;

public function __construct(){
    $this->CI =& get_instance();
    $this->CI->load->model('Sites');  
}

public function get_records()
{
    $results = $this->CI->Sites->get_activeRecords();
 // other lines of code
 }

Use this piece of code

Upvotes: 0

Sourabh Kumar Sharma
Sourabh Kumar Sharma

Reputation: 2807

use this key word to access $IC, and use the below code:

class something-todo {

protected $CI;

public function __construct(){
    $this->CI =& get_instance();
    $this->CI->load->model('Sites');  
}

public function get_records()
{
    $results = $this->CI->Sites->get_activeRecords();
 // other lines of code
 }

Upvotes: 0

Saty
Saty

Reputation: 22532

Your code is not working because you create instance

 $this->$CI =& get_instance();

and you use

 $CI->load->model('Sites'); // it is $this->$CI

Write way of creating instance how it is use

  $this->CI =& get_instance();

so your code will be

public function __construct(){
    $this->CI =& get_instance();
    $this->CI->load->model('Sites');  
}


public function get_records()
{
    $results = $this->CI->Sites->get_activeRecords();
 // other lines of code
 }

Upvotes: 1

Related Questions