【发布时间】:2016-08-09 02:54:44
【问题描述】:
我有一段代码,它是 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 构造函数,我不知道吗?还是我的代码有问题而我没有看到?
【问题讨论】:
-
阅读manual!
-
@csabinho 真的吗?究竟在哪里?因为我已经阅读了它并且没有发现任何关于这个问题的帮助
-
$this->config->load('filename'); Where filename is the name of your config file, without the .php file extension. -
@csabinho 您在阅读我的代码并理解问题所在吗?我正在尝试仅使用文件名加载文件,正如您在传递给构造函数并默认初始化为
['config' => 'navigation']的参数$params中看到的那样,这不起作用,这就是问题 -
params的值是4个元素的数组还是1个元素的数组?如果是 4 个元素,CI 的工作方式与手册中的说明相同。
标签: php codeigniter codeigniter-3 php-5.6