...

View Full Version : Issue with global Keyword



spyke01
12-01-2008, 10:56 PM
Hi guys im having an issue with the global keyword. Whats happening is that when i try to bring a global variable into the functions scope it just doesnt see the variable inside the file.

Ill post the full code below but heres the jist of whats happening:

a function is called to load a module, we give it prefix if we know it as well as the name of the hook that we want to call, we also pass in any paramaters it needs and we tell it if we want data back or not.

The function then looks up all modules that match our search in the database, if we find results we include the actual modules php file (this file is where things break) and then call our hook.

Heres what a call looks like


$content .= loadModules(MODULE_TYPE_PAYMENT_OPTION, "", "showButton", "", "1");


Heres the code for the function we just called


//=========================================================
// Load module files so we can call them
//=========================================================
function loadModules($type, $prefix, $hookToCall, $hookArguments, $returnResult) {
global $DBTABLEPREFIX;
$hookFunctionName = "";

$extraSql = "";
$extraSql = ($prefix == "") ? "" : " AND m.modules_prefix = '" . $prefix . "'";

$sql = "SELECT ma.modules_assoc_active, m.modules_viewing_permisions, m.modules_prefix FROM `" . $DBTABLEPREFIX . "modules_assoc` ma LEFT JOIN `" . $DBTABLEPREFIX . "modules` m ON m.modules_id = ma.modules_assoc_module_id WHERE ma.modules_assoc_company_id = '" . $_SESSION['companyid'] . "' AND m.modules_type = '" . $type . "'" . $extraSql . ";";
$result = mysql_query($sql);
echo $sql . "<br />";

if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
// We now need to cycle through our modules that match our search, if they are active
if ($row['modules_assoc_active'] == ACTIVE) {
// Piece together our total function name

// Get our module directory structure
$moduleHolderDirectory = ($row['modules_viewing_permisions'] == MODULE_PERM_PUBLIC) ? "global" : $_SESSION['companyid'];

switch ($type) {
case MODULE_TYPE_PAGE:
$moduleTypeDirectory = "pages";
break;
case MODULE_TYPE_PAYMENT_OPTION:
$moduleTypeDirectory = "payment_options";
break;
case MODULE_TYPE_GRAPH:
$moduleTypeDirectory = "graphs";
break;
case MODULE_TYPE_REPORT:
$moduleTypeDirectory = "reports";
break;
case MODULE_TYPE_REGISTER:
$moduleTypeDirectory = "registers";
break;
case MODULE_TYPE_USER:
$moduleTypeDirectory = "users";
break;
case MODULE_TYPE_INVENTORY:
$moduleTypeDirectory = "inventory";
break;
case MODULE_TYPE_ANALYTICS:
$moduleTypeDirectory = "analytics";
break;
}

// Include our module file
$fullModulePath = "modules/" . $moduleHolderDirectory . "/" . $moduleTypeDirectory . "/" . $row['modules_prefix'] . ".php";
include($fullModulePath);

// Call our hook function
if ($returnResult == 1) {
/*
$returnVar = "";
eval("\$returnVar = " . $hookFunctionName . ";");
return $returnVar;
*/
if ($hookArguments != "") {
$returnVar = call_user_func($row['modules_prefix'] . '_' . $hookToCall);
}
else {
$returnVar = call_user_func($row['modules_prefix'] . '_' . $hookToCall);
}
return $returnVar;
}
else {
eval($hookFunctionName . ";");
}
}
}
mysql_free_result($result);
}
}



Heres whats breaking


//====================================
// Basic Module information
//====================================
$name = "Accept Cash";
$description = "Adds Cash to the list of accepted payment types.";
$developer = "Paden Clayton";
$version = "1.08.11.25";
$prefix = "cash";
$type = MODULE_TYPE_PAYMENT_OPTION;
$viewingPermissions = MODULE_PERM_PUBLIC;
$seperateTab = 0;
$buttonText = "Pay By Cash";

//====================================
// Show Button hook
//====================================
function cash_showButton() {
global $menuvar, $prefix, $buttonText;

$content = "
<a href=\"" . $menuvar['PAYMENT'] . "&action=module&prefix=" . $prefix . "\" class=\"button\"><span>" . $buttonText . "</span></a>";

return $content;
}


The items in all caps are defined variables that get used throghuout the system. Ive could get this working by passing the variables as parameters but on a large scale this completely defeats the purpose of making a modular system and really prohibits future changes.

Any ideas on how to fix this issue?

Fumigator
12-01-2008, 11:57 PM
You're not showing the code where variable $DBTABLEPREFIX gets initialized/defined/assigned a value, but I suspect this is not actually a variable but a constant, which is a specific value defined once and used throughout a system.

If this is true, then you don't need to declare it a global variable, as all constants are automatically global to all functions. Your mistake may be using the dollar sign on the constant's name; they do not use the dollar sign.

You can tell if it's a constant because it will be defined like this somewhere:



define("GREETING", "Hello you.");


http://us3.php.net/manual/en/language.constants.php

spyke01
12-02-2008, 02:21 PM
the $DBTABLEPREFIX is a variable declared in _db.php file which also does the connection to the database, its not a constant, however probably should be. The constants in my script are all located in the constants.php which along with the _db.php file gets included on every page (via a single header file) i can post this if necessary. im staring to think that this might end up having to become a class instead of being done like it is, however this might cause quite a bit of trouble down the road

Fumigator
12-02-2008, 03:26 PM
Ok well... I can't tell what's actually wrong. Generally you want to avoid global variables as much as possible and pass variables to a function instead. In 98% of the cases doing this creates more modular and portable code.

Yes, if you make it a full-fledged class you'll get even more portable, as you can pack everything that interacts with the class into the class (as long as it's written correctly).

spyke01
12-02-2008, 08:34 PM
thanks, i think im going to go the class route, i believe i can access the class variables without creating an instance by specifying the namespace



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum