0

The basis of the problem. I have a config file located at /random_folder/ranom_f/config/_config.php

I want to be able to access that file from /random/functions/php/functions.php

The config file will not always be located in /random_folder/random_f/config/_config.php so I have to figure out a way to know where the config file is located.

My attempted solution is to set a superglobal (like in $_SERVER) that gives the "root" of the software I am creating.

Does anyone have a better solution / know how I can set a superglobal like that?

blexshelwy
  • 56
  • 4

3 Answers3

3

From Here

Static class variables can be referenced globally, e.g.:

class myGlobals {

   static $myVariable;

}

function a() {

  print myGlobals::$myVariable;

}
Community
  • 1
  • 1
Bijan
  • 7,737
  • 18
  • 89
  • 149
2

Just define a constant. Those are, by definition, superglobals (available to all scopes)

 define('FOO', 'some val');

 function yo() {
     echo FOO;
 }

The next best thing is simply using $GLOBALS (not recommended but also available to all scopes)

 $foo = 'some val';

 function yo() {
     echo $GLOBALS['foo'];
 }
Machavity
  • 30,841
  • 27
  • 92
  • 100
1

I would go with a helper function. First of all i suppose that your config file path is relative to something, right?

The helper:

function get_config_path($params){
 /* code to select the path */
 return 'path'; /* path is a string */
}

In your main (index.php) file:

@include get_config_path($params).'/_config.php'; /* Add your standar config file name */
@include 'functions.php'

/* Use them */

That pretty much gives the main idea.

I hope it helps.

ChrisL
  • 90
  • 7