-
edo
Newbie
-
Posts: 7
Threads: 3
Joined: Jul 2015
Reputation:
0
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.
-
wolfgang1983
Senior Member
-
Posts: 627
Threads: 271
Joined: Oct 2014
Reputation:
7
(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!
-
mwhitney
Posting Freak
-
Posts: 1,101
Threads: 4
Joined: Nov 2014
Reputation:
95
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);
-
edo
Newbie
-
Posts: 7
Threads: 3
Joined: Jul 2015
Reputation:
0
(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.
-
edo
Newbie
-
Posts: 7
Threads: 3
Joined: Jul 2015
Reputation:
0
(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.
|