Custom libraries in header files and configuration files?

I have a library in a code igniter that looks like class MyClass($options = array())

File Myclass.php

I have a file ( config/Myclass.php) that looks like

$Myclass = array(
  'something' => 'value',
  'another'   => 'value'
);

As I thought, an array should pass $Myclasswhen I initialize my class, but apparently not?

What do I need to do to fix this?

+3
source share
3 answers

AH I found the answer,

The array inside your configuration file should be named $config.

The file name should also be a lowercase representation of the library file name.

eg

FILE OR: Somelibrary.php
LIB CONTENT: class Somelibrary($options = array()){...
CONF FILE: Somelibrary.php
CONTENT:$config = array('something' => 'value');

+2

, , , , .

var myObject = new MyClass(); // default settings

var myObject = new MyClass(array('something' => 'value2')); // override only "something"

, ; , :

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...
    }
}
+1

2013 , - . , , , , .

  • , . Config form_validation.php ( , CI_Form_validation , )

  • , , , codeigniter v2.1.3. :

    public function __construct( $config = array() )
    {
      parent::__construct($config);
    }
    
0

All Articles