I am using codeigniter. I defined a library in code-igniter and is expecting a parameter in its constructor.This is my library code -
################# [My Library Code Test_lib.php ] ########################
<?php
class Test_lib
{
var $params;
public function __construct($params)
{
$this->params = $params;
echo $this->params;
}
}
In codeigniter documentation, it is mentioned that you can pass the parameter in the second argument. So,I am initializing it from controller as below -
<?php
class Test_cont extends CI_Controller {
function __construct()
{
parent::__construct();
}
function test()
{
$params = "abc";
$this->load->library("test_lib",$params);
}
}
I am getting following error -
A PHP Error was encountered Severity: Warning Message: Missing argument.....
Please assist.
All of the available libraries are located in your system/libraries/ directory. In most cases, to use one of these classes involves initializing it within a controller using the following initialization method: $this->load->library('class_name');
You will load it using: $this->load->library('flavors/chocolate'); You may nest the file in as many subdirectories as you want. Additionally, multiple libraries can be loaded at the same time by passing an array of libraries to the load function.
$this used to get/load things like helpers, libraries, database, global variables in your extended class. For example $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->load->library("pagination");
$params
must be an array. From the documentation:
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);
In your case, you want to do something like this:
function test()
{
$params[] = "abc"; // $params is an array
$this->load->library("test_lib",$params);
}
You just need to modify the $params from a variable to array. hope this will work
function test()
{
$params = array(1=>'abc');
$this->load->library('test_lib',$params);
}
Class Test_lib
################# [My Library Code Test_lib.php ] ########################
<?php
class Test_lib {
var $parameter1;
var $parameter2;
var $parameter3;
public function __construct($parameters) {
/* array assoc */
$this->parameter1 = $parameters['argument1'];
$this->parameter2 = $parameters['argument2'];
$this->parameter3 = $parameters['argument3'];
/* object */
$oParameters = (object) $parameters;
$this->parameter1 = $oParameters->argument1;
$this->parameter2 = $oParameters->argument2;
$this->parameter3 = $oParameters->argument3;
}
}
Class Controller
<?php
class Test_cont extends CI_Controller {
function __construct() {
parent::__construct();
}
function test() {
$data = array( // array assoc
'argument1' => 'String 1',
'argument2' => 'String 2',
'argument3' => 'String 3'
);
$this->load->library("Test_lib", $data);
return $this->Test_lib->parameter1;
}
echo $this->test(); // String 1
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With