我使用CI作为我的框架,我不知道这段代码的错误是什么
defined('BASEPATH') OR exit('No direct script access allowed');
class RajaOngkir extends CI_Controller {
private $api_key, $base_url;
public function __construct() {
// Pastikan bahwa PHP mendukung cURL
if (!$this->is_curl_exists()) {
log_message('error', 'cURL Class - PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.');
}
$this->_ci=&get_instance();
var_dump($this->_ci);
$this->_ci->load->config('rajaongkir', TRUE);
// Pastikan Anda sudah memasukkan API Key di application/config/rajaongkir.php
if ($this->_ci->config->item('api_key', 'rajaongkir') == "") {
log_message("error", "Harap masukkan API KEY Anda di config.");
} else {
$this->api_key = $this->_ci->config->item('api_key', 'rajaongkir');
$this->base_url = $this->_ci->config->item('api_base_url', 'rajaongkir');
}
}
当我使用web浏览器访问它时,它给我一个错误
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/rajaongkir.php
Line Number: 22
Fatal error: Call to a member function config() on a non-object in C:\xampp\htdocs\Koen_CI\application\controllers\rajaongkir.php on line 22
我认为错误是在我调用这个->_ci=&get_instance时出现的。因为它返回空值。
如何修复它?
发布于 2015-03-04 22:55:08
您需要在类中定义_ci变量:
private $api_key, $base_url;
至
private $api_key, $base_url, $_ci;
但不要这么做。正如其他人在评论中提到的,如果你在一个控制器中,直接使用$this->something
而不是$this->_ci
。仅当您在CI结构之外(例如,在助手文件中)时,才应使用get_instance()
。
https://stackoverflow.com/questions/28854841
复制相似问题