Welcome Guest, Not a member yet? Register   Sign In
Custom library config file
#1

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.
Reply
#2

(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/ge...rrors.html

http://www.codeigniter.com/user_guide/ge...og_message

And for writing file may be use codeigniter file helper

http://www.codeigniter.com/user_guide/he...elper.html
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#3

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); 
Reply
#4

(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/ge...rrors.html

http://www.codeigniter.com/user_guide/ge...og_message

And for writing file may be use codeigniter file helper

http://www.codeigniter.com/user_guide/he...elper.html

wolfgang1983,

Thank you for your answer.

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

Cheers.
Reply
#5

(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.
Reply
#6

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB