Original Post
I have a problem with PHP global variables. Two functions, Register and Read, access the global variable $_GLOBALS["UT_MODULE"]. Register writes to it, and Read reads from it. A Process function includes a PHP file, then calls te Read function. The included file contains calls to Register which give information about it. During calls to Register, the value of the global variable is correct. However, during subsequent calls to Read, the value is incorrect (and empty). Are PHP global variables not actually global? I am trying to get a simple unit testing code (as follows) to work: By loading www.website.dom/unittest.php, complete unit testing should be performed using the above test functions and the results displayed (along with the unit tests of other modules). But I am stuck in the process of transmitting information about the module name and test functions from the unit test file to the actual unittest.php system.
// file : myphpfile.ut.php
RegisterModule( "myphpfile.ut.php", "MyPHP File" );
require_once( "myphpfile.inc.php" );
function testFoo( ) { /* test Foo */ }
RegisterPart( "Foo", testFoo );
function testBar( ) { /* test Bar */ }
RegisterPart( "Bar", testBar );
// file : unittest.php
/* Registers a module from a given file.
*
* Stores the data in the UT_MODULE and UT_FILE. Erases
* all parts registered by previous modules.*
*/
function RegisterModule( $file, $module ) {
$_GLOBALS["UT_FILE"] = $file;
$_GLOBALS["UT_MODULE"] = $module;
// Displays correct values.
echo "Module: " . $_GLOBALS["UT_FILE"] . " name: " . $_GLOBALS["UT_MODULE"] . "
";
$_GLOBALS["UT_PARTS"] = array( );
}
/* Registers a module part.
*
* Stores the part along the previous ones, in order,
* in the UT_PARTS global variables.
*/
function RegisterPart( $name, $func ) {
$parts = & $_GLOBALS["UT_PARTS"];
assert( !$parts[$name] );
$parts[$name] = $func;
}
/* Creates a module information structure, useful for
* returning from a GetUnitTestData function.
*
* Uses the information stored by RegisterModule and
* RegisterPart in the global variables UT_MODULE, UT_FILE
* and UT_PARTS;
*/
function & UnitTest_IdentifyModule( ) {
$data["file"] = & $_GLOBALS["UT_FILE"];
$data["name"] = & $_GLOBALS["UT_MODULE"];
$data["parts"] = & $_GLOBALS["UT_PARTS"];
// Displays "Module: name: "
echo "Module: " . $_GLOBALS["UT_FILE"] . " name: " . $_GLOBALS["UT_MODULE"] . "
";
return $data;
}