如何动态设置代码编写器库构造函数


how to dynamically set codeigniter library constructor

我试图动态设置一个编码构造器,但我有困难。我在控制器中有:

$arguments=array('login'=>'***','pass'=>'***');  ;                   
$this->load->library('mailer', $arguments);
$phpmail = new Mailer;
$phpmail->sendmail('***', 'bob', 'hi', 'there');

构造函数的第一行看起来像:

public function __construct($login,$pass)

但是我得到了以下内容:

Message: Missing argument 1 for Mailer::__construct(), 

你知道我做错了什么吗?

提前感谢,

比尔

From the docs:

在库加载函数中,你可以动态地传递数据数组通过第二个参数,它将被传递给你的类构造函数:

$params = array('type' => 'large', 'color' => 'red');
$this->load->library('Someclass', $params);

如果您使用此功能您必须设置您的类构造函数以期望数据:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Someclass {
    public function __construct($params)
    {
        // Do something with $params
    }
}
?>

第一个参数就是你传递的数组。使用$params['login']$params['pass']代替。