• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom libraries with custom config files ... how?

#1
[eluser]Aaron Wallentine[/eluser]
Hello,

The CodeIgniter documentation states thusly:

Quote:Passing Parameters When Initializing Your Class

In the library loading function you can dynamically pass data via the second parameter and it will be passed to your class constructor:
$params = array('type' => 'large', 'color' => 'red');

$this->load->library('Someclass', $params);

If you use this feature you must set up your class constructor to expect data:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Someclass {

    function Someclass($params)
    {
        // Do something with $params
    }
}
?>

You can also pass parameters stored in a config file. Simply create a config file named identically to the class file name and store it in your application/config/ folder. Note that if you dynamically pass parameters as described above, the config file option will not be available.

Anyone know more specifics on this? I tried this out, making a file in application/config identically named to my library's file name, and added some parameters there; but how are they supposed to be named, and how do you access them from the library? I can't seem to find any documentation on this.

Since it doesn't say anything, I assumed that the parameters in the config file would be available as an array passed into the constructor, but that doesn't seem to be the case.

I tried naming my parameters in the config file as members of a $config array, as well as an array named after my class. But neither shows up when the class constructor is called.

Anyone have anything on this?

Thanks

#2
[eluser]tomcode[/eluser]
In Your case the config file needs to be named someclass.php.


Inside You have the standard config notation

Code:
$config['var_key'] = '"Variable Value"';


And in the library

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Someclass {

    var $var_key = 'to be overriden';

    function Someclass($params = false)
    {
        // Do something with $params

       $this->init($params);
    }

    function init($params = false)
    {
        foreach($params as $key => $value)
        {
              $this->$key = $value;
        }
    }
}

#3
[eluser]Aaron Wallentine[/eluser]
Thanks, Tom.

That's basically what I did.

Here's what I have:

file: application/config/AW_forms.php
Code:
$config['reqd_img_url'] = 'images/reqd_star.gif';
$config['error_img_url'] = 'images/err_cross.gif';

file: application/libraries/AW_forms.php
Code:
class AW_forms {
    
    protected  $reqd_img_url = '/images/reqd_star.gif';
    protected  $error_img_url = '/images/err_cross.gif';
    
    function __construct($params = array()) {
    
        echo "hello I am the AW_forms constructor ...";
        echo "<pre>\n\$params:\n";
        print_r($params);
        echo "</pre>\n";
        
    }

file: application/config/autoload.php
Code:
$autoload['libraries'] = array('database', 'AW_forms');
// ...
$autoload['config'] = array('AW_forms');

I even tried auto-loading the config file just in case that would make a difference.

But in my output I'm consistently getting:
Code:
hello I am the AW_forms constructor ...

$params:
Array
(
)

Indicating that those params from the config file aren't being passed into the constructor.

Does it make a difference that I'm autoloading the library instead of loading it manually?
Or perhaps the fact that the second letter of the library/class/config file is capitalized?

Or that I'm using __construct() instead of the PHP 4 way of making constructor functions? (I'm using PHP 5 so that shouldn't matter, I'd think)


I'll experiment with those things and see if they make a difference.

#4
[eluser]tomcode[/eluser]
Guess You should have the config filename lowercase : aw_forms.php

the CI default is :

1. Library : First letter uppercase, rest lowercase
2. config : lowercase

If CI works with lcfirst() then your config file needs to be named aW_forms.php (would be very bizzare).

autoloading the config file is senseless since CI is passing the data via the constructor, and only the data of the this specific config file.

Just to make sure : You have NOT exchanged in config/config.php:

Code:
$config['subclass_prefix'] = 'MY_';
//with
$config['subclass_prefix'] = 'AW_';

By the way, you can do also :
Code:
// a one liner array | object echo
echo '<pre>$params: ' .print_r($params, true) ."</pre>\n";

EDIT : you might try without autoloading, to be sure Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.