Reputation: 92581
I have a library in code igniter that looks like class MyClass($options = array())
The file is Myclass.php
I have a file (config/Myclass.php
) that looks like
$Myclass = array(
'something' => 'value',
'another' => 'value'
);
Which I thought should pass the $Myclass
array in when I initialize my class, but apparently not?
What do I need to do to fix it?
Upvotes: 3
Views: 1615
Reputation: 1345
Fast-forward to 2013, someone is still having problems with this. So my situation was the same but slightly different, so I thought I'd try to save someone else some time.
I was naming my config file after my extended class, that was wrong. The Config file should always be form_validation.php, (this is because eventually it is handed to the CI_Form_validation class and that's what it expects)
Thanks to the folks who answered this question above, I realized that I had to pass the config to the parent class and using codeigniter v2.1.3 I did it as follows:
public function __construct( $config = array() )
{
parent::__construct($config);
}
Upvotes: 0
Reputation: 92581
AH I found the answer,
The array inside your config file must be called $config
.
The name of the file must also be a lower case representation of the library file name.
e.g
LIB FILE: Somelibrary.php
LIB CONTENTS: class Somelibrary($options = array()){...
CONF FILE: somelibrary.php
CONF CONTENTS: $config = array('something' => 'value');
Upvotes: 2
Reputation: 14550
The way this usually works is that you pass in an array of options you wish to override, or pass in nothing to use the defaults.
var myObject = new MyClass(); // default settings
var myObject = new MyClass(array('something' => 'value2')); // override only "something"
Honestly, I wouldn't create your own file in config without a good reason; instead, just put the defaults in your class definition, and then override in your constructor:
class MyClass {
var $default_options = array(
'something' => 'value',
'another' => 'value',
);
var $options = array();
function MyClass($override_options = array())
{
$this->options = array_merge($this->default_options, $override_options);
// your code here...
}
}
Upvotes: 1