使用此功能我想创建一个PHP导航:
function loadPage($pagename) {
if (!isset($pagename) || !file_exists($pagename . '.php')) {
include FRONTPAGE . '.php';
} else {
include $pagename . '.php';
}
}
在我的索引中,我有以下内容:
require 'includes/classes/core.php';
$core = new SH_Core();
我可以访问$ core中的所有其他类,例如$ core-> database-> newConnection(); 我也应该能够在包含的文件中使用它。但我不能:
Notice: Undefined variable: core in C:\Users\Development\Development applications\localhost\htdocs\Shuze\frontpage.php on line 4
Notice: Trying to get property of non-object in C:\Users\Development\Development applications\localhost\htdocs\Shuze\frontpage.php on line 4
Fatal error: Call to a member function newConnection() on a non-object in C:\Users\Development\Development applications\localhost\htdocs\Shuze\frontpage.php on line 4
答案 0 :(得分:3)
您需要授予您的功能访问$ core:
function loadPage( $pageName ) {
global $core;
//rest of code
}
您也可以放在包含文件的顶部。或者,您可以使用超全局$ GLOBALS ['core']。