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.

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

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致