A little help needed with creating a library - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: A little help needed with creating a library (/showthread.php?tid=16365) Pages:
1
2
|
A little help needed with creating a library - El Forum - 03-04-2009 [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 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 A little help needed with creating a library - El Forum - 03-04-2009 [eluser]TheFuzzy0ne[/eluser] You're missing the dollar sign prefix on the call to $this->initialize(). Those typos are a bummer. 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()) A little help needed with creating a library - El Forum - 03-04-2009 [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. 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()) The typo is because I was hurrying to go see Watchmen when typing the sample code and isn't in the normal code 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'); A little help needed with creating a library - El Forum - 03-04-2009 [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 { And here's my config file... Code: $config = array( A little help needed with creating a library - El Forum - 03-05-2009 [eluser]xwero[/eluser] [quote author="matrim" date="1236210789"] Code: if($config > 0) forgot the count function. A little help needed with creating a library - El Forum - 03-05-2009 [eluser]darkhouse[/eluser] lol can't believe we missed that. A little help needed with creating a library - El Forum - 03-05-2009 [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( /application/library/MyClass.php Code: public $user = null; Controller Code: $this->load->library('MyClass'); A little help needed with creating a library - El Forum - 03-05-2009 [eluser]matrim[/eluser] And straight after I posted the last post I realized it was the " if (isset($this->$key))" stopping it A little help needed with creating a library - El Forum - 03-05-2009 [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. A little help needed with creating a library - El Forum - 03-05-2009 [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 = ""; |