Jump to content
  • Advertisement
Sign in to follow this  
ToohrVyk

[web] PHP globals

This topic is 4559 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
Advertisement
Solution: stop messing with those $_GLOBALS and use the global keyword. That's what it's made for anyway. Thanks anyway :)

Share this post


Link to post
Share on other sites
As I recall right, you have to use the global keyword to access non-local vars inside PHP functions.

EDIT: Damned, now I got already beaten by the OP authors itself :)

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!