Welcome Guest, Not a member yet? Register   Sign In
A little help needed with creating a library
#1

[eluser]matrim[/eluser]
I'm trying to create a library but am having problems to pass config values to it

I want to be able to pass values in 2 ways

1. user passes array of configuration parameters to set the values
or
2. user creates a config file (/system/application/config/myclass.php) and this is automatically picked up

So what I have so far is something like

Code:
class MyClass
{
   public user = "";
   public pass = "";

   function MyClass($config = array())
   {
      if($config > 0)
      {
         this->initialize($config)
      }
   }

   function initialize($config = array())
   {
       foreach ($config as $key => $val)
    {
       if (isset($this->$key))
       {
                $this->$key = $val;
       }
    }
   }

   function doSomething()
   {
   }
}
But it doesn't work either way.

I previously had it working passing a $params array directly into the constructor without the initialize function but want to be able to do it via a config file.

The docs say you can do this but I can't find an example of it to see if I'm doing it correctly. The above way is from me looking over the ci_email class in the build in libraries and trying to copy that
#2

[eluser]TheFuzzy0ne[/eluser]
You're missing the dollar sign prefix on the call to $this->initialize(). Those typos are a bummer. Wink

My SI CAPTCHA library loads settings from a file. You might be able to learn something from the constructor.

Code:
function Si_captcha($config=array())
{
    $this->CI =& get_instance();
    $this->CI->load->library('session'); // Load the session library in case it's not already loaded.
    
    // Load the default configuration.
    require_once(APPPATH . 'config/securimage'.EXT);
    if (isset($SI_config))
    {
        $this->initialize($SI_config);
        unset($SI_config);
    }
    
    // Now override with the users config if it's been supplied.
    $this->initialize($config);
    // Suh-weeeet...
}
#3

[eluser]matrim[/eluser]
[quote author="TheFuzzy0ne" date="1236211343"]You're missing the dollar sign prefix on the call to $this->initialize(). Those typos are a bummer. Wink

My SI CAPTCHA library loads settings from a file. You might be able to learn something from the constructor.

Code:
function Si_captcha($config=array())
{
    $this->CI =& get_instance();
    $this->CI->load->library('session'); // Load the session library in case it's not already loaded.
    
    // Load the default configuration.
    require_once(APPPATH . 'config/securimage'.EXT);
    if (isset($SI_config))
    {
        $this->initialize($SI_config);
        unset($SI_config);
    }
    
    // Now override with the users config if it's been supplied.
    $this->initialize($config);
    // Suh-weeeet...
}
[/quote]

The typo is because I was hurrying to go see Watchmen when typing the sample code and isn't in the normal code Big Grin

So from looking over your code it seems you import the config file with a require_once. But the docs seem to indicate that it's possible to get the config file by just naming it to the same name as the class.

If you have to import it yourself would it be better practice to load the config file with something like $this->CI->config->load('myconfig');
#4

[eluser]darkhouse[/eluser]
I don't see anything wrong off hand with your code. My config files load automatically. Here's some sample working code from my External library.

Code:
class CI_External {
    
    var $CI;

    function CI_External($groups = array()){    
        $this->CI =& get_instance();
        
        if(is_array($groups) && count($groups) > 0){
            //parse groups
        }
    }
    
    //...etc
}

And here's my config file...

Code:
$config = array(
    'all' => array(
        'css' => array(
            'css/reset.css',
            'css/shortcuts.css',
            array('.png_bg {behavior: url(iepngfix.htc)}', 'ie6_custom')
        ),
        'js' => array(
            'js/functions.js',
            array('var base_url = "'.base_url().'";', 'custom'),
            array('js/ie_png_fix.js', 'ie6')    
        )
    ),
    'default' => array('css' => 'css/master.css'),
    'admin' => array('css' => 'css/admin/master.css'),
    'beta' => array('css' => array('css/beta.css', 'css/960.css'))
);
#5

[eluser]xwero[/eluser]
[quote author="matrim" date="1236210789"]
Code:
if($config > 0)
      {
         this->initialize($config)
      }
[/quote]
forgot the count function.
#6

[eluser]darkhouse[/eluser]
lol can't believe we missed that.
#7

[eluser]matrim[/eluser]
It's still not working here's the actual code from my app


config file

/application/config/myclass.php
Code:
$config = array(
  'user' => 'user',
  'pass' => 'pass'
);

/application/library/MyClass.php
Code:
public $user = null;
    public $pass = null;

    function MyClass ($config = array()) {
        $this->CI =& get_instance();
        
        if (count($config) > 0)
        {
            $this->initialize($config);
        }
    }

    function initialize($config = array())
    {

        foreach ($config as $key => $val)
        {
            if (isset($this->$key))
            {
                $this->$key = $val;
            }
        }
    }

Controller

Code:
$this->load->library('MyClass');
#8

[eluser]matrim[/eluser]
And straight after I posted the last post I realized it was the " if (isset($this->$key))" stopping it
#9

[eluser]TheFuzzy0ne[/eluser]
Haha, I was just about to say that. But I'm not sure why it should be the problem, unless of course you wanted to add variables that weren't already defined.

EDIT That was a dumb question. isset() fails because the default value of that variable is NULL. Changing it to FALSE, or '' should do the trick.
#10

[eluser]matrim[/eluser]
[quote author="TheFuzzy0ne" date="1236268757"]Haha, I was just about to say that. But I'm not sure why it should be the problem, unless of course you wanted to add variables that weren't already defined.[/quote]

It was returning false because I set the variables in my class to Null. There are 2 ways for me to fix it.

1. remove isset check
2. when declaring my values don't make them null instead declare them like pass = "";




Theme © iAndrew 2016 - Forum software by © MyBB