user1592380
user1592380

Reputation: 36227

how to dynamically set codeigniter library constructor

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

Upvotes: 0

Views: 1266

Answers (1)

Waleed Khan
Waleed Khan

Reputation: 11467

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.

Upvotes: 2

Related Questions