如何动态设置codeigniter库构造函数 [英] how to dynamically set codeigniter library constructor

查看:111
本文介绍了如何动态设置codeigniter库构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态设置一个codeigniter构造函数,但我有困难。我有我的控制器:

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

我的构造函数第一行如下:

  public function __construct($ login,$ pass)

我得到以下:

 消息:Mailer :: __ construct(),
的缺少参数1 code>



任何想法我做错了什么?





比尔

解决方案

文档


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

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

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

如果您使用此功能
,则必须设置类构造函数以获取数据: / p>

 <?php if(!define('BASEPATH'))exit('不允许直接脚本访问); 

class Someclass {

public function __construct($ params)
{
//使用$ params
执行操作}
}

?>


第一个参数是您传递的精确数组。改为使用 $ params ['login'] $ params ['pass'] $ b

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

解决方案

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.

这篇关于如何动态设置codeigniter库构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆