Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load library by passing parameter to constructor in codeigniter

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.

like image 358
piyush Avatar asked May 18 '12 15:05

piyush


People also ask

Can we load library in model CodeIgniter?

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');

Which function is used to load a library in CodeIgniter?

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.

What is $this in CodeIgniter?

$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");


3 Answers

$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);
}
like image 101
Colin Brock Avatar answered Nov 09 '22 08:11

Colin Brock


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);
}
like image 27
Moyed Ansari Avatar answered Nov 09 '22 08:11

Moyed Ansari


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

  }
like image 7
antelove Avatar answered Nov 09 '22 07:11

antelove