5

I am really new to Codeigniter, and just learning from scratch. In the CI docs it says:

$params = array('type' => 'large', 'color' => 'red');
$this->load->library('Someclass', $params);
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Someclass {

    public function __construct($params)
    {
        // Do something with $params
    }
}

Can you give me simple example how to pass data from controller to external library using array as parameters? I'd like to see a simple example.

7
  • 1
    And the example is in your question ..$params = array('type' => 'large', 'color' => 'red'); $this->load->library('Someclass', $params); does it Commented Oct 18, 2012 at 11:12
  • @BhuvanRikka: I don't agree with or understand your first comment, can you clarify? Why shouldn't you pass parameters to the constructor? Commented Oct 18, 2012 at 11:21
  • @WesleyMurch We cannot access constructor directly,right! Like,passing parameters into it from a uri. I never tried it though. And i've never seen anyone doing so. I may be completely wrong. The moment i saw it in the OPs question made me feel like that. can we do it? Please explain & sorry for my bizzare comment Commented Oct 18, 2012 at 11:30
  • CI libraries are just PHP classes, you can do anything you want with them, including calling __construct manually. I can't understand your question at all, of course you can pass in arguments to the constructor. Commented Oct 18, 2012 at 11:32
  • @WesleyMurch TFYI. I was seeing it as a method which can't be called from a uri. I forgot the basic thing that it can be called manually inside the constructor :O Commented Oct 18, 2012 at 11:37

2 Answers 2

8

All Codeigniter "library" constructors expect a single argument: an array of parameters, which are usually passed while loading the class with CI's loader, as in your example:

$params = array('type' => 'large', 'color' => 'red');
$this->load->library('Someclass', $params);

I'm guessing you're confused about the "Do something with $params" part. It's not necessary to pass in any params, but if you do you might use them like this:

class Someclass {
    public $color = 'blue'; //default color
    public $size = 'small'; //default size
    public function __construct($params)
    {
        foreach ($params as $property => $value)
        {
            $this->$property = $value;
        }
        // Size is now "large", color is "red"
    }
}

You can always re-initialize later like so, if you need to:

$this->load->library('Someclass');
$this->Someclass->__construct($params);

Another thing to note is that if you have a config file that matches the name of your class, that configuration will be loaded automatically. So for example, if you have the file application/config/someclass.php:

$config['size'] = 'medium';
$config['color'] = 'green';
// etc.

This config will be automatically passed to the class constructor of "someclass" when it is loaded.

Sign up to request clarification or add additional context in comments.

Comments

2

In libraries directory create one file Someclass_lib.php

Here is your Library code

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Someclass_lib
{
    public  $type       =   '';
    public  $color      =   '';

    function Someclass_lib($params)
    {
        $this->CI   =&  get_instance();
        $this->type =   $params['type'];
        $this->color    =   $params['color'];
    }
}

Use this code when you want to load library

$params = array('type' => 'large', 'color' => 'red'); 
$this->load->library('Someclass_lib', $params);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.