Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments when loading custom CodeIgniter library

I'm trying to implement a class I've written as CodeIgniter library.

Somehow I can't get CI's load() method to pass multiple arguments to the class's constructor function.

My class is designed to get 3 arguments, 2 arrays and one optional string.

The constructor looks somewhat like this:

public function __construct($array, $array,$string=""){
/** code **/
}

The relevant part from the controller:

function index(){
  $array1 = array('key1'=>'value','key2'=>'value');
  $array2 = array('key1'=>'value','key2'=>'value');
  $string = "value";
  $params = array($array1,$array2,$string);
  $this->load->library("MyClass",$params);
}

Loading the controller generates this error :

Message: Missing argument 2 for MyClass::__construct()

This is really puzzling me. It seems the first argument gets sent fine and then it chokes on the second argument. Any clues on why this is happening will be greatly appreciated.

like image 206
Andrei Avatar asked Aug 13 '10 21:08

Andrei


1 Answers

You need to modify your class constructor to handle the passed data as described here:

https://www.codeigniter.com/user_guide/general/creating_libraries.html

public function __construct($params)
{
    $array1 = $params[0];
    $array2 = $params[1];
    $string = $params[2];

    // Rest of the code
}
like image 110
spidEY Avatar answered Oct 15 '22 09:10

spidEY