php - 如何使用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');
}
解决方案
初始化库时:
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('someclass', $params);
你的图书馆:
class Someclass {
public function __construct($params)
{
echo $params['type']; // large
}
}
注意:CI只能通过一个参数,所以如果要发送多个参数,必须通过一个参数作为数组发送,如上图所示。
推荐阅读
- javascript - 如何找出哪个生态系统.json 文件用于在 pm2 中启动进程
- html - CSS Animation 仅适用于 Firefox 和 Chrome,但不适用于 Apple 设备(Safari?)
- c# - 在 .net core 6 中配置连接字符串
- android - 折线图java android
- python - 运行单个消费者的多个实例(Python/Rabbitmq)
- node.js - 从 Node.js 中的 AWS Secrets Manager 检索密钥
- video - 使用 -ffmpeg- 将音频混合并叠加到 4x4 视频中
- django - 无法从视图 django 中的 url 访问文件
- python - 恼人的噪音扬声器树莓派零
- julia - 无法使用 Dates.jl 添加两个时间组件