Home > Software design >  Why is my array not being accessed by an included file?
Why is my array not being accessed by an included file?

Time:09-29

I'm working on a site from scratch. Partially as practice and to help a friend. One of the things I'm doing is creating the menu dynamically off of plugins and modules that we create. My current config defines the base files and then the user plugin is supposed to be able to push more data into the array. What I have currently is

<?php
function Deny() {
if(!defined("__directaccess")) {
  header("Location: /");
  }
}
Deny();
$Plugins = array("Bootstrap", "MySQL", "User");
$menuItems = array (
  "Home"  => "/",
);

And that's inside my config.php file. Within my User.class.php file which is successfully loaded with the plugin loader I've built I have

require_once(__root.'/config.php');
$menu = array (
  "User Panel"  => "/index.php?page=user",
  "Logout" => "/index.php?page=login&module=logout",
);
print_r($menuItems);

$menuItems Gives that it's not defined. I've checked with a simple echo "Included"; script inside the config.php file to ensure that it's being included, but at the $menuItems we fail out. What I want to happen is after the Menu has been generated I'll be able to push more data into the menuItems array so that modules/plugins can add to the menu as well and use variables within those functions to make the navigation seamless. Any help would be appreciated as I'm stumped as I don't understand why it can't access the array.

Load order to help see what's going on index.php

LoadPlugins($Plugins);
LoadModule("TopNav");
LoadPages();

loader.php

session_start();
define("__directaccess", true);

require_once("config.php");
//Loads All Class Functions
function LoadClasses() {
    foreach(glob(__class.'/*.class.php') as $class) {
    if(file_exists($class)) {
        require_once($class);
        }
    }
}

function LoadPlugins($Plugins) {
foreach($Plugins as $Plugin) {
  LoadPlugin($Plugin);
    }
}


function LoadPages() {
if(isset($_GET['page'])) {
        if(file_exists(__root. '/includes/' . $_GET['page'] . '.php')) {
            require_once(__root. '/includes/' . $_GET['page'] . '.php');
    } else {
        echo "Page not Found"; //Make a 404 Error Page
    }
}
if(!isset($_GET['page'])) {
            if(file_exists(__root . '/includes/homepage.php')) {
                require_once(__root . '/includes/homepage.php');
            }
    }
}


function LoadModule($moduleName, $page="index") {
    $path = __modules . '/' . $moduleName . '/' . $page . '.php';
        if(file_exists($path)) {
            require_once($path);
        } else {
            echo "Failed to Load module " . $moduleName;
        }
}

function LoadPlugin($pluginName) {
    $path = __plugins . '/' . $pluginName . '/' . $pluginName . '.class.php';
    if(file_exists($path)) {
        require_once($path);
    } else {
        echo "Failed to load plugin " . $pluginName;
    }
}

The $menuItems array is first created in the config.php file and then the plugins are loaded. The thought is that the plugins should be able to access variables from within the config.php file with that format.

Full User.class.php

$menu = array (
  "User Panel"  => "/index.php?page=user",
  "Logout" => "/index.php?page=login&module=logout",
);

print_r($menuItems);

function __isOnline() {
if(isset($_SESSION['username'])) {
  return true;
  } else {
  return false;
  }
}

class UserFunctions extends webConn {
  public function userDetails($arg, $username) {
    $query = <<<SQL
    SELECT * FROM account WHERE username = :username
SQL;
    $resource = $this->db->prepare( $query );
    $resource->execute( array (
      ":username" => $username,
    ));
    $result = $resource->fetch(PDO::FETCH_ASSOC);
    return $result[$arg];
  }
}
$userFunction = new UserFunctions();

config.php with SQL Database info Removed

<?php
function Deny() {
if(!defined("__directaccess")) {
  header("Location: /");
  exit();
  }
}
//Deny();
$Plugins = array("Bootstrap", "MySQL", "User");
$menuItems = array (
  "Home"  => "/",
);





//Create a root directory base name to reference
define('__root', dirname(__file__));
//Create Class Reference Global Variable
define('__class', __root . '/classes');
//Create Module reference
define('__modules', __root.'/modules');
//Create Plugin reference
define('__plugins', __root.'/plugins');

CodePudding user response:

When you call require/include inside a function, the scope is the function itself.

For example the following function

function LoadClasses() {
    require_once($class);
}

is equivalent to

function LoadClasses() {
    print_r($menuItems);
}

That's why $menuItems is null. To fix the problem, you can use global keyword to always refer a global variable.

gloabl $menuItems;
print_r($menuItems);
  •  Tags:  
  • php
  • Related