为什么$params从配置文件中获取值,而不是默认值


Why $params gets the values from configuration file and not the value set as default?

我有这是一段代码,这是CI 3.1自定义库的一部分:

class NavigationMenu
{
    protected $CI;
    public function __construct($params = ['config' => 'navigation'])
    {
        // this is where I read $params as an array of 10 values
        // and it shouldn't be since $params has only one key = config
        var_dump($params);
        $this->CI =& get_instance();
        $this->CI->load->helper('url');  
        $this->CI->config->load($params['config'], true);
        $this->CI->load->model('nav_model', 'nav');
    }
    ....
}

文件'navigation.php '有以下代码:

$config['navigation_open']          = '<ul class="nav">';
$config['navigation_close']         = '</ul>';
$config['item_open']                = '<li>';
$config['item_open_active_class']   = 'active';
我注意到传递给构造函数的$params读为:
array (size=10)
  'navigation_open' => string '<ul class="nav">' (length=16)
  'navigation_close' => string '</ul>' (length=5)
  'item_open' => string '<li>' (length=4)
  'item_open_active_class' => string 'active' (length=6)

为什么不读为?

array (size=1)
  'config' => string 'navigation' (length=10)

编辑

我没有使用库,这意味着没有创建对象,而是自动加载库,简单如:

config/autoload.php
$autoload['config'] = ['navigation'];

这将导致每当我点击应用程序的index.php文件时都会调用类构造函数。

这是CI的默认行为吗?从PHP构造函数和我不知道它?或者我的代码有问题,而我没有看到它?

您应该使用$this->CI->config->set_item($params['config'], true)代替。请仔细阅读本文档