Passing arguments when loading custom CodeIgniter library

Andrei picture Andrei · Aug 13, 2010 · Viewed 16k times · Source

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.

Answer

spidEY picture spidEY · Aug 14, 2010

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
}