0

I have following files structure.

config.php
header.php
page.php
footer.php

The header.php includes the config.php file and and page.php includes header.php:

header.php

include ('config.php');

page.php:

include ('header.php');
include ('footer.php');

Now on this page.php if I want to access any function which is in config.php, it gives error as it cannot access the functions in config.php file.

It works if I include config.php file in the page.php also. However in this way I have to include that file in each of the page.

Is there anyway to include that file in the header.php file so that I don't have to include in each page?

Edit: config.php is very simple and has only connection string. Here is the content:

define('SOME_CONSTANT', 'xxx');

try {
    $db = new PDO("odbc:Driver="..);        
} catch(PDOException $e){
    echo $e->getMessage();
    die();
}
sam
  • 1,027
  • 3
  • 11
  • 21
  • can you show config.php – Vision Coderz Oct 05 '17 at 11:38
  • Based on what you have said, it should work the way you want, unless the function is namespaced. – Starx Oct 05 '17 at 11:39
  • By default you should be able to use functions from `config.php` in `page.php`, what version of PHP are you using? Also as a side note you should use `include_once` instead of `include` as you are going to run into problems later. Since you mentioned you have to include `config.php` in both `header.php` and `page.php` for it to work, you are including it twice. And sometimes this can cause problems. – Antoniu Livadariu Oct 05 '17 at 11:39
  • @iCoders I edited the question, please see. – sam Oct 05 '17 at 11:45
  • `config.php` you added, does not have any function. – Starx Oct 05 '17 at 11:46
  • @Starx it has only db connection string so that I don't have to make db connection in each file – sam Oct 05 '17 at 11:47
  • Then why are you saying `Now on this page.php if I want to access any function which is in config.php, it gives error as it cannot access the functions in config.php file.` in your question, if you don't have any functions? – Starx Oct 05 '17 at 11:48
  • can you please show page.php – Shubham Saini Oct 05 '17 at 11:49
  • @Starx I apologize, function was not the right word, I perhaps wanted to say functionality. I just need to establish the database connection through that file. – sam Oct 05 '17 at 11:52
  • Once you include the file the connection will establish, don't worry about that. Now if you want to access the variable `$db` from `config.php` you should use `global $db;` and that will use the global `$db` variable which is declared in `config.php` – Antoniu Livadariu Oct 05 '17 at 11:53

1 Answers1

0

Your functions in config.php must be public

Ex.: config.php

public function func_name() {

    // code

}