doubenggua9430 2013-03-20 16:42
浏览 24
已采纳

如何动态设置codeigniter库构造函数

I am trying to dynamically set a codeigniter constructor, but am having difficulty. I have in my controller:

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

My constructors first line looks like:

public function __construct($login,$pass)

but I'm getting the following:

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

Any ideas what I am doing wrong?

Thanks in advance,

Bill

  • 写回答

1条回答 默认 最新

  • doujia1679 2013-03-20 16:46
    关注

    From the docs:

    In the library loading function you can dynamically pass data as an array via the second parameter and it will be passed to your class constructor:

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

    If you use this feature you must set up your class constructor to expect data:

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

    The first argument is the exact array you passed. Use $params['login'] and $params['pass'] instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗