Welcome Guest, Not a member yet? Register   Sign In
Create class and access associated conf file
#1

[eluser]Dermot B[/eluser]
Hi,

I am new to CI and I have been reading the user guide. I want to create my own library file. The user guide says that if you want to use a config file to pass values then create one with the same name and put it in the config folder. What is does not say is how to access this from your class. I know this might be a stupid questions but I am not sure how I should access this... can anyone tell me?

Thanks for your time
Dermot
#2

[eluser]Thorpe Obazee[/eluser]
Code:
//config
$config['key'] = 'value';

Have you tried this in your class?:

Code:
$this->key;
#3

[eluser]Dermot B[/eluser]
Thank for helping me.

I have tried that. There is an issue in that $this is not available in the a custom class you must get an instance. Either approach has not worked

Code:
$config['page_header'] = "something or other";

Code:
$this->page_header;

or

Code:
$CI =& get_instance();
echo $CI->page_header;

any other ideas?
#4

[eluser]Cro_Crx[/eluser]
Within the class I'm fairly sure you can't use $this. You need to use either $CI-> or $this->CI ... I'm not sure which, so try both Tongue
#5

[eluser]Colin Williams[/eluser]
The constructor is passed the config array.

Code:
class Custom_class {

  function Custom_class($config = array())
  {
    // $config array in config/custom_class.php is passed as argument
  }

}
#6

[eluser]Dermot B[/eluser]
Thanks so much Colin, that is exactly what I have been looking for. I looked in the user guide and couldn't find that. You have been a great help.

I appreciate it.
Dermot

[quote author="Colin Williams" date="1247476249"]The constructor is passed the config array.

Code:
class Custom_class {

  function Custom_class($config = array())
  {
    // $config array in config/custom_class.php is passed as argument
  }

}
[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB