2

I'm working with PHP to build an MVC in order to understand how one works.

After establishing the basic framework, I had the idea to define common variables in a file that is included in my initialization file (index.php) and accessible by all subsequent files. After researching, and a ridiculous amount of trial and error, I've found a way to accomplish this.

Visual:

##common.php
# a file of variables used in my project.

$myvar1 = value1;
$myvar2 = value2;
etc...

I initially tried to reference the variables in the common.php file as if they were defined on the page in which they were to be used, yet discovered this did not work.

Visual:

##page.php
# another page in the same project
# where common.php is included.

//get the value of $myvar1

print $myvar1; // *this does not return value1*

Now I am referencing the variables as part of the $GLOBALS array.

##page.php
print $GLOBALS['myvar1'] // *returns value1*

These are variables, thus I did not use define(constant, value).

Is my method correct, is there another correct way to do this, or perhaps I'm totally off base?

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • by subsequent files do you mean *included* files, or files which the user later visits? – Dave Mar 11 '13 at 00:21
  • if the former, can you show what code you're using to actually include the other file, and where you put that code? – Dave Mar 11 '13 at 00:23
  • @Dave they are included files. I include the `common.php` file on the core `MVC.php` file and all future files will be includes of this file. Basically all files will have the `common.php` included. –  Mar 11 '13 at 00:29
  • @Dave as I'm learning the MVC, I may not have the best language to describe the flow. The `common.php` file is included in the core `MVC.php` file. Any page I where I want to reference `common.php`'s variables will be loaded by the controller to the MVC. Sorry if I'm not clear. Should I post some more code? –  Mar 11 '13 at 00:39

3 Answers3

2

You should have a class, called Registry to store all your shared variables.

This class is of course a singleton shared in all your MVC framework. You set the variables in your bootstrap function like this:

$Registry->save('yourVar','yourValue');

And then you get this variable whenever you are in your MVC:

$Registry->get('yourVar');

Of course that fact that you need the istance of this class in all your app brings the problem of Global State. You can find more information in this relevant question: If Singletons are bad then why is a Service Container good?

Community
  • 1
  • 1
dynamic
  • 46,985
  • 55
  • 154
  • 231
  • Thank you for pointing me in the right direction. I need to do some studying before I know how to properly implement this class; know of any simple examples I could look at? Also could you tell me why referencing the `$GLOBALS` array is wrong? –  Mar 11 '13 at 01:07
  • because "Global State" is considered a bad behaviour, if you google it: http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/ you'll find many sources – dynamic Mar 11 '13 at 01:26
0

$GLOBALS — References all variables available in global scope
Simply An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

<?php
function globaltest() {
    $foo = "local variable";

    echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
    echo '$foo in current scope: ' . $foo . "\n";
}

$foo = "Gaint Global content";
globaltest();
?>

$foo in global scope: Gaint Global content
$foo in current scope: local variable

This is what my friend does for configuration file..

<?php
$conf['conf']['foo'] = 'this is foo';
$conf['conf']['fooB'] = 'this is fooB';

function foobar() {
    global $conf;
    var_dump($conf);
}

foobar();

?>

result is..

array 'conf' => array 'foo' => string 'this is foo' (length=11) 'fooB' => string 'this is fooB' (length=12)

and Remember to avoid *print '$GLOBALS = ' . var_export($GLOBALS, true) . "\n";*

Goops!

internals-in
  • 4,798
  • 2
  • 21
  • 38
-2

If they are global variables you need to declare them in the function you want to access them from. e.g.

From main file

 $glbTesting = False ; 

from your common file

 function lg($txt) {
    global $glbTesting;  // this is required otherwise it cannot access the variable called externally

   if ( $glbTesting == True ) 
   {
     echo $txt."<BR>" ; 
   }
   return True;
 }
acutesoftware
  • 1,091
  • 3
  • 14
  • 33
  • I assume my answer was downvoted because of the use of globals, but in all fairness the question was exactly how to reference globals. – acutesoftware Mar 11 '13 at 01:04