我正在從頭開始在一個網站上作業。部分作為練習和幫助朋友。我正在做的一件事是根據我們創建的插件和模塊動態創建選單。我當前的配置定義了基本檔案,然后用戶插件應該能夠將更多資料推送到陣列中。我目前擁有的是
<?php
function Deny() {
if(!defined("__directaccess")) {
header("Location: /");
}
}
Deny();
$Plugins = array("Bootstrap", "MySQL", "User");
$menuItems = array (
"Home" => "/",
);
這在我的 config.php 檔案中。在使用我構建的插件加載器成功加載的 User.class.php 檔案中,我有
require_once(__root.'/config.php');
$menu = array (
"User Panel" => "/index.php?page=user",
"Logout" => "/index.php?page=login&module=logout",
);
print_r($menuItems);
$menuItems給出它沒有定義。我已經檢查echo "Included";了 config.php 檔案中的一個簡單腳本,以確保它被包含在內,但是$menuItems我們失敗了。我想要發生的是在生成選單之后,我將能夠將更多資料推送到 menuItems 陣列中,以便模塊/插件也可以添加到選單中,并使用這些函式中的變數來使導航無縫。任何幫助將不勝感激,因為我很困惑,因為我不明白為什么它無法訪問陣列。
加載訂單以幫助查看發生了什么
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;
}
}
首先$menuItems在config.php檔案中創建陣列,然后加載插件。想法是插件應該能夠使用該格式從 config.php 檔案中訪問變數。
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');
uj5u.com熱心網友回復:
在require/include函式內部呼叫時,作用域就是函式本身。
例如下面的函式
function LoadClasses() {
require_once($class);
}
相當于
function LoadClasses() {
print_r($menuItems);
}
這就是為什么$menuItems為空。要解決此問題,您可以使用global關鍵字來始終參考全域變數。
gloabl $menuItems;
print_r($menuItems);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/510076.html
標籤:php
上一篇:如何在2022年使用WSL的Windows上的intellij中使用gradle和Kotlin設定docker-composespringbootwebserver/mysql專案并進行熱多載?
