CodeIgniter Forums
Custom library config file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Custom library config file (/showthread.php?tid=63978)



Custom library config file - edo - 12-29-2015

Hi All,

I have a third party library which uses a external config file like this

application/library/api/API.php:
Code:
require_once "config.php";
error_log("Config var 1:" . $config_var); // Pure PHP Ok, CI Ok

class API {
    function __construct () {
        global $config_var;
        error_log("Config var 2:" . $config_var); // Pure PHP Ok, CI BLANK!!
    }
    function doSomething (param1, param2) {
        global $config_var_filename;
        global $config_var_number;
        $file = fopen($config_var_filename . '/' . param1, 'a+'); // Error
        fwrite($file, $config_var_number + param2); // More errors
        fclose($file); // And more errors
    }
}

I've setup a library stub this way

application/library/LibAPI:
Code:
include dirname(__FILE__) . '/api/API.php';

class LibAPI extends API {
    function __construct () {
        parent::__construct();
    }
}

Which I use in my controller like this

application/controllers/MyController:
Code:
class MyController extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('LibAPI');
        $this->load->helper(array('url', 'form'));
        ....
    }
    public function index () {
        $api = new LibAPI();
        $api->doSomething('.TXT', 1); // ERROR!!!
        ...
    }
    ...


The problem, as you can see in the included comments, is that under CI config parameters are not kept and, obviously, the API fails. I need to maintain the API code unchanged, so I cannot introduce CI specific code in it. What could I do?

Thank you.

Edo.


RE: Custom library config file - wolfgang1983 - 12-29-2015

(12-29-2015, 09:59 PM)edo Wrote: Hi All,

I have a third party library which uses a external config file like this

application/library/api/API.php:
Code:
require_once "config.php";
error_log("Config var 1:" . $config_var); // Pure PHP Ok, CI Ok

class API {
    function __construct () {
        global $config_var;
        error_log("Config var 2:" . $config_var); // Pure PHP Ok, CI BLANK!!
    }
    function doSomething (param1, param2) {
        global $config_var_filename;
        global $config_var_number;
        $file = fopen($config_var_filename . '/' . param1, 'a+'); // Error
        fwrite($file, $config_var_number + param2); // More errors
        fclose($file); // And more errors
    }
}

I've setup a library stub this way

application/library/LibAPI:
Code:
include dirname(__FILE__) . '/api/API.php';

class LibAPI extends API {
    function __construct () {
        parent::__construct();
    }
}

Which I use in my controller like this

application/controllers/MyController:
Code:
class MyController extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('LibAPI');
        $this->load->helper(array('url', 'form'));
        ....
    }
    public function index () {
        $api = new LibAPI();
        $api->doSomething('.TXT', 1); // ERROR!!!
        ...
    }
    ...


The problem, as you can see in the included comments, is that under CI config parameters are not kept and, obviously, the API fails. I need to maintain the API code unchanged, so I cannot introduce CI specific code in it. What could I do?

Thank you.

Edo.

Codeigniter has own built in error log

http://www.codeigniter.com/user_guide/general/errors.html

http://www.codeigniter.com/user_guide/general/errors.html#log_message

And for writing file may be use codeigniter file helper

http://www.codeigniter.com/user_guide/helpers/file_helper.html


RE: Custom library config file - mwhitney - 12-30-2015

I have a feeling the require_once "config.php" may be part of the issue, but if you can't change the API code, there's only so much you can do. Have you tried using the instance created by the loader:

PHP Code:
$this->libapi->doSomething('.TXT'1); 

instead of this:

PHP Code:
$api = new LibAPI();
$api->doSomething('.TXT'1); 



RE: Custom library config file - edo - 12-30-2015

(12-29-2015, 10:58 PM)wolfgang1983 Wrote:
(12-29-2015, 09:59 PM)edo Wrote: Hi All,

I have a third party library which uses a external config file like this

application/library/api/API.php:
Code:
require_once "config.php";
error_log("Config var 1:" . $config_var); // Pure PHP Ok, CI Ok

class API {
function __construct () {
global $config_var;
error_log("Config var 2:" . $config_var); // Pure PHP Ok, CI BLANK!!
}
function doSomething (param1, param2) {
global $config_var_filename;
global $config_var_number;
$file = fopen($config_var_filename . '/' . param1, 'a+'); // Error
fwrite($file, $config_var_number + param2); // More errors
fclose($file); // And more errors
}
}

I've setup a library stub this way

application/library/LibAPI:
Code:
include dirname(__FILE__) . '/api/API.php';

class LibAPI extends API {
function __construct () {
parent::__construct();
}
}

Which I use in my controller like this

application/controllers/MyController:
Code:
class MyController extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('LibAPI');
$this->load->helper(array('url', 'form'));
....
}
public function index () {
$api = new LibAPI();
$api->doSomething('.TXT', 1); // ERROR!!!
...
}
...


The problem, as you can see in the included comments, is that under CI config parameters are not kept and, obviously, the API fails. I need to maintain the API code unchanged, so I cannot introduce CI specific code in it. What could I do?

Thank you.

Edo.

Codeigniter has own built in error log

http://www.codeigniter.com/user_guide/general/errors.html

http://www.codeigniter.com/user_guide/general/errors.html#log_message

And for writing file may be use codeigniter file helper

http://www.codeigniter.com/user_guide/helpers/file_helper.html

wolfgang1983,

Thank you for your answer.

I know about logging and file handling in CI, but that's not my problem.

Cheers.


RE: Custom library config file - edo - 12-30-2015

(12-30-2015, 09:00 AM)mwhitney Wrote: I have a feeling the require_once "config.php" may be part of the issue, but if you can't change the API code, there's only so much you can do. Have you tried using the instance created by the loader:

PHP Code:
$this->libapi->doSomething('.TXT'1); 

instead of this:

PHP Code:
$api = new LibAPI();
$api->doSomething('.TXT'1); 

mwhitney,

Thank you for responding.

The class constructor initializes some instance vars, so I have to instantiate it.

Cheers.


RE: Custom library config file - mwhitney - 12-31-2015

(12-30-2015, 07:25 PM)edo Wrote: mwhitney,

Thank you for responding.

The class constructor initializes some instance vars, so I have to instantiate it.

Cheers.

When you call $this->load->library('LibAPI'), the loader instantiates the class and assigns the instance to $this->libapi. You should either use the loader and use the instance bound to $this->libapi, or you should not use the loader and instantiate it yourself. You're trying to do both, which is one of the potential causes of at least some of your problems.