Current location: Hot Scripts Forums » Programming Languages » PHP » Issue with global Keyword

Issue with global Keyword

Reply
  #1 (permalink)  
Old 12-01-08, 05:54 PM
spyke01 spyke01 is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
Issue with global Keyword

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
php Code:
  1. $content .= loadModules(MODULE_TYPE_PAYMENT_OPTION, "", "showButton", "", "1");

Heres the code for the function we just called
php Code:
  1. //=========================================================
  2. // Load module files so we can call them
  3. //=========================================================
  4. function loadModules($type, $prefix, $hookToCall, $hookArguments, $returnResult) {
  5.     global $DBTABLEPREFIX;
  6.     $hookFunctionName = "";
  7.    
  8.     $extraSql = "";
  9.     $extraSql = ($prefix == "") ? "" : " AND m.modules_prefix = '" . $prefix . "'";
  10.    
  11.     $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 . ";"; 
  12.     $result = mysql_query($sql);
  13.     echo $sql . "<br />";
  14.    
  15.     if (mysql_num_rows($result) > 0) {
  16.         while ($row = mysql_fetch_array($result)) {
  17.             // We now need to cycle through our modules that match our search, if they are active
  18.             if ($row&#91;'modules_assoc_active'] == ACTIVE) {
  19.                 // Piece together our total function name
  20.                
  21.                 // Get our module directory structure
  22.                 $moduleHolderDirectory = ($row&#91;'modules_viewing_permisions'] == MODULE_PERM_PUBLIC) ? "global" : $_SESSION['companyid'];
  23.                
  24.                 switch ($type) {
  25.                     case MODULE_TYPE_PAGE:
  26.                         $moduleTypeDirectory = "pages";
  27.                         break;
  28.                     case MODULE_TYPE_PAYMENT_OPTION:
  29.                         $moduleTypeDirectory = "payment_options";
  30.                         break;
  31.                     case MODULE_TYPE_GRAPH:
  32.                         $moduleTypeDirectory = "graphs";
  33.                         break;
  34.                     case MODULE_TYPE_REPORT:
  35.                         $moduleTypeDirectory = "reports";
  36.                         break;
  37.                     case MODULE_TYPE_REGISTER:
  38.                         $moduleTypeDirectory = "registers";
  39.                         break;
  40.                     case MODULE_TYPE_USER:
  41.                         $moduleTypeDirectory = "users";
  42.                         break;
  43.                     case MODULE_TYPE_INVENTORY:
  44.                         $moduleTypeDirectory = "inventory";
  45.                         break;
  46.                     case MODULE_TYPE_ANALYTICS:
  47.                         $moduleTypeDirectory = "analytics";
  48.                         break;
  49.                 }
  50.                
  51.                 // Include our module file
  52.                 $fullModulePath = "modules/" . $moduleHolderDirectory . "/" . $moduleTypeDirectory . "/" . $row&#91;'modules_prefix'] . ".php";
  53.                 include($fullModulePath);            
  54.                
  55.                 // Call our hook function
  56.                 if ($returnResult == 1) {
  57.                     /*
  58.                     $returnVar = "";
  59.                     eval("\$returnVar = " . $hookFunctionName . ";");
  60.                     return $returnVar;
  61.                     */
  62.                     if ($hookArguments != "") {
  63.                         $returnVar = call_user_func($row&#91;'modules_prefix'] . '_' . $hookToCall);
  64.                     }
  65.                     else {
  66.                         $returnVar = call_user_func($row&#91;'modules_prefix'] . '_' . $hookToCall);
  67.                     }
  68.                     return $returnVar;
  69.                 }
  70.                 else {
  71.                     eval($hookFunctionName . ";");
  72.                 }
  73.             }
  74.         }
  75.         mysql_free_result($result)
  76.     }
  77. }


Heres whats breaking
php Code:
  1. //====================================
  2. // Basic Module information
  3. //====================================
  4. $name = "Accept Cash";
  5. $description = "Adds Cash to the list of accepted payment types.";
  6. $developer = "Paden Clayton";
  7. $version = "1.08.11.25";
  8. $prefix = "cash";
  9. $type = MODULE_TYPE_PAYMENT_OPTION;
  10. $viewingPermissions = MODULE_PERM_PUBLIC;
  11. $seperateTab = 0;
  12. $buttonText = "Pay By Cash";
  13.  
  14. //====================================
  15. // Show Button hook
  16. //====================================
  17. function cash_showButton() {
  18.     global $menuvar, $prefix, $buttonText;
  19.    
  20.     $content = "
  21.                 <a href=\"" . $menuvar&#91;'PAYMENT'] . "&action=module&prefix=" . $prefix . "\" class=\"button\"><span>" . $buttonText . "</span></a>";
  22.  
  23.     return $content;
  24. }

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-01-08, 06:08 PM
wirehopper's Avatar
wirehopper wirehopper is offline
Community Liaison
 
Join Date: Feb 2006
Posts: 1,563
Thanks: 2
Thanked 25 Times in 25 Posts
I would place common constants such as MODULE_TYPE_PAYMENT_OPTION in a separate file that consists of all the necessary defines to support the interface, and then establish the practice that all modules must include the file with the interface constants.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 12-02-08, 09:18 AM
spyke01 spyke01 is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
already got that, the constants are located in a file called constants, i can post these if you need me to.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 12-02-08, 03:20 PM
wirehopper's Avatar
wirehopper wirehopper is offline
Community Liaison
 
Join Date: Feb 2006
Posts: 1,563
Thanks: 2
Thanked 25 Times in 25 Posts
I recommend you include or require the constants file in the file that references them.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 12-02-08, 03:34 PM
spyke01 spyke01 is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
i currently use include on them, i think what is happening is that when the module file is included the global keyword becomes confused and cant properly find the right variable, i think the only way to solve this will be to create a class out of the module
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 12-03-08, 03:18 PM
wirehopper's Avatar
wirehopper wirehopper is offline
Community Liaison
 
Join Date: Feb 2006
Posts: 1,563
Thanks: 2
Thanked 25 Times in 25 Posts
Quote:
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.]
Using parameters won't prohibit future changes and it may make your code more secure, more robust, and easier to maintain.

You can always extend a parameter list with default parameters, like so:

PHP Code:
function phpFunction ($parm1='default',$parm2='',$parm3=0)
{} 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
DBD - Global Symbols Issue cunixgroup Perl 3 08-21-07 10:35 AM
RSS Using RSS2Html Script VKX PHP 1 10-16-06 06:27 PM
Issue Tracking and Task management SlackeR Script Requests 8 10-05-05 05:29 PM
Redirection back to a page from form submit DAL Perl 11 03-21-05 03:45 PM
PHP Error Fairnie PHP 8 06-26-04 08:15 AM


All times are GMT -5. The time now is 11:18 PM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.