Welcome Guest, Not a member yet? Register   Sign In
Pass variables from config to library
#1

[eluser]codex[/eluser]
I'm trying to pass vars from a config file to a library, but I can't get it to work. What I have so far (The config file is autoloaded):

In config
Code:
$config['path_imagemagick']    = '/usr/bin/convert ';
$config['path_tempdir']        = '/home/name/public_html/temp/';
$config['path_photosdir']    = '/home/name/public_html/photos/';

The library
Code:
class CI_ImgResizer_IM {
    
    var $CI;
    var $imagick         = $this->config->item('path_imagemagick');
    var $temppath         = $this->config->item('path_tempdir');
    var $newpath         = $this->config->item('path_photosdir');
    
    /**
     * Constructor
     *
     */    
    function CI_ImgResizer_IM()
    {    
        $this->CI =& get_instance();
        log_message('debug', "ImgResizer_IM Class Initialized");
    }
    
// the functions here

}

What am I missing or doing wrong??
#2

[eluser]Michael Wales[/eluser]
You are missing the CI object in your variable declarations:

Code:
var $CI;
    var $imagick         = $this->CI->config->item('path_imagemagick');
    var $temppath         = $this->CI->config->item('path_tempdir');
    var $newpath         = $this->CI->config->item('path_photosdir');
#3

[eluser]codex[/eluser]
[quote author="walesmd" date="1193430522"]You are missing the CI object in your variable declarations:

Code:
var $CI;
    var $imagick         = $this->CI->config->item('path_imagemagick');
    var $temppath         = $this->CI->config->item('path_tempdir');
    var $newpath         = $this->CI->config->item('path_photosdir');
[/quote]

I had already tried that (read it in another post), but it didn't work. Still doesn't...


Maybe I'm wrong, but isn't '$this->CI' only available AFTER the constructor?
#4

[eluser]Michael Wales[/eluser]
I thought about that... if that is the case, well you obviously can't just use $this->config (unless you feel like writing a config class...

I'm not the most gangster OOP programmer, but what if you tried this:

Code:
var $CI;
    var $imagick;
    var $temppath;
    var $newpath;

    function __construct() {
        $this->CI =& get_instance();
        $this->imagick         = $this->CI->config->item('path_imagemagick');
        $this->temppath         = $this->CI->config->item('path_tempdir');
        $this->newpath         = $this->CI->config->item('path_photosdir');
    }

You will then just have to remember to refer to them as class variables.
#5

[eluser]codex[/eluser]
Yaay, that works. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB