$ CI在功能php中不可用

时间:2015-05-20 06:10:55

标签: php codeigniter

不确定这是否已经回答,但我似乎无法找到正确的答案

我正在创建一个库,我的库正在使用现有模型。我在__construct之外使用$ CI时遇到了问题。

这是我的代码片段。

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
 }

无论何时我调用此库,我都会在get_records()函数中收到$ CI错误。

不确定还能做什么。

谢谢这是重复的,感谢重定向。否则,请提前感谢任何答案。

3 个答案:

答案 0 :(得分:1)

您的代码无效,因为您创建了实例

import csv
import os
import glob

rows = list()
path_to_files = r'C:/csv-directory'
file_list = ['file1.csv', 'file2.csv']

# Step 1 & 2 - read all files into one big collection
#              and ignore any blank lines

# If you have a large list of files:
# for filename in glob.iglob('{}/*.csv'.format(path_to_files)): 
#   with open(filename) as f:

for filename in file_list:
   with open(os.path.join(path_to_files, filename)) as f:
      reader = csv.reader(f, delimiter=',')
      rows += list(reader)

# Step 3 & 4 - Perform some operation on the
#              big CSV file, and provide output
#              as CSV file
with open(os.path.join(path_to_files, 'result.csv'), 'w') as f:
   writer = csv.writer(f, delimiter=',')
   for row in rows:
       # do something with row
       writer.writerow(row)

你使用

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

编写创建实例的方式

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

所以你的代码将是

  $this->CI =& get_instance();

答案 1 :(得分:0)

使用此关键字访问$ IC,并使用以下代码:

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
 }

答案 2 :(得分:0)

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
 }

使用这段代码