Sign in to follow this  

[web] PHP globals

This topic is 4342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
// 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"] . " <br/>";
	
	$_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"] . " <br/>";
	
	return $data;
}

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.

Share this post


Link to post
Share on other sites
Use of the global keyword is easier, but if you wanted to use the "globals" array, it's called $GLOBALS not $_GLOBALS.

This would be why it's not working - the superglobal is called $GLOBALS not $_GLOBALS.

But it's far better to just say

global $UT_FILE, $UT_MODULE;

Then refer to them as normal vars.

Mark

Share this post


Link to post
Share on other sites
Quote:
Original post by markr
Use of the global keyword is easier, but if you wanted to use the "globals" array, it's called $GLOBALS not $_GLOBALS


Argh. You're right, I got my brain messed up by $_REQUEST...

Share this post


Link to post
Share on other sites

This topic is 4342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this