2017-06-28 55 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']; 

    } 
}