2017-06-28 56 views
1

我试图库类中加载自定义配置文件加载自定义配置。我遇到了配置值返回null的问题。笨,麻烦库类

配置文件: 'couriers.php'

$config['ups'] = 'some keys'; 

库文件: '/library/Track/Ups.php'

class Ups { 

    public $ci; 

    public function __contruct() { 

     $this->ci =& get_instance(); 
     $this->ci->config->load('couriers'); 
    } 

    public function GetUPSKey() { 

     return config_item('ups'); 

    } 
} 

我得到一个空响应。

任何帮助表示赞赏。

回答

0

测试完代码后。我发现只有一个问题是constructor的简单错字。你想念拼写它。所以构造函数永远不会被调用。改变它并检查。其他的事情都很好

public function __construct() { 

     $this->ci =& get_instance(); 
     $this->ci->config->load('couriers'); 
    } 
0

/*类*/

class Ups { 

    protected $ci; 
    protected $config; 

    public function __construct() { 

     $this->ci =& get_instance(); 

     // Loads a config file named couriers.php and assigns it to an index named "couriers" 
     $this->ci->config->load('couriers', TRUE); 

     // Retrieve a config item named ups contained within the couriers array 
     $this->config = $this->ci->config->item('ups', 'couriers'); 
    } 

    public function GetUPSKey() { 

     return $this->config['key']; 

    } 
} 

/*配置(couriers.php)*/

$config['ups'] = array(
    'key' => 'thisismykey', 
    'setting2' => 'etc' 
); 

// Additional Couriers etc 
$config['dhl'] = array(
    'key' => 'thisismykey', 
    'setting2' => 'etc' 
); 
0

你必须加载配置和其指向的对象,并使用该而返回:

class Ups { 

    public $ci; 

    public function __contruct() { 

     $this->ci =& get_instance(); 
     $this->couriers_config = $this->ci->config->load('couriers'); 
    } 

    public function GetUPSKey() { 

     return $this->couriers_config['ups']; 

    } 
}