Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

[web] PHP globals

Started by ToohrVyk Jan 27, 2006 at 6:39 AM 3 replies 950+ views
Original Post
ToohrVyk
ToohrVyk
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"] . " 
"; $_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; }
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.
ToohrVyk
ToohrVyk
Solution: stop messing with those $_GLOBALS and use the global keyword. That's what it's made for anyway. Thanks anyway :)
haegarr
haegarr
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 :)
markr
markr
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
ToohrVyk
ToohrVyk
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...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.