首页 > 解决方案 > 如何使用codeigniter将参数传递给自定义库?

问题描述

我正在codeigniter中创建一个自定义库,我想在构造函数中传递参数。任何解决方案都适用!

function __construct( $iteration_count_log2, $portable_hashes )
    {
        $this->itoa64 = 
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

    if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
        $iteration_count_log2 = 8;
    $this->iteration_count_log2 = $iteration_count_log2;

    $this->portable_hashes = $portable_hashes;

    $this->random_state = microtime() . uniqid(rand(), TRUE); // removed getmypid() for compatibility reasons
}

这是加载库的代码

public function __construct() {
    parent::__construct();
    $this->load->library('PasswordHash');
}

标签: phpcodeigniter

解决方案


来自文档:https ://www.codeigniter.com/user_guide/general/creating_libraries.html#passing-parameters-when-initializing-your-class

初始化库时:

$params = array('type' => 'large', 'color' => 'red');

$this->load->library('someclass', $params);

你的图书馆:

class Someclass {

        public function __construct($params)
        {
                echo $params['type']; // large
        }
}

注意:CI只能通过一个参数,所以如果要发送多个参数,必须通过一个参数作为数组发送,如上图所示。


推荐阅读