当我无法调用方法时,我遇到了问题。结构类似于:
/www
/classes
/class.php
/pages
/page.php
/index.php所以我想从page.php文件中调用方法。自动加载程序是在index.php文件中配置的。怎样才是正确的做法呢?我在index.php中创建了对象,在page.php中调用了方法,但是它抛出了Fatal error: Call to a member function indexAboutMe() on a non-object in...
我认为,问题是index.php文件不将对象变量传递给page.php。
我使用来自另一个类的方法来包含页面(包含了过程很好的html,但是不能调用方法,因为对象变量是空的。),这会造成这个错误吗?
代码:
index.php
<?php
$page = $_GET['page'];
if (empty($page)){$page = 'sakums';}
function __autoload($class_name) {
include './classes/' . $class_name . '.php';
}
$active = new activePage();
$db = new database();
?>
//then there is bunch of plain html code
<?php
$pageSwitcher = new pageSwitcher();
$pageSwitcher->pageLoader($page);
?> //this is page switching method, which includes file from pages folder 包括页的类:
<?php
/*
*class for page loading depending on active page.
*/
class pageSwitcher
{
public function pageLoader($page)
{
switch ($page)
{
case 'sakums':
include_once './pages/sakums.php';
break;
}
}
}?>和无法接收对象变量的sakums.php:
//html here
<?php $db->indexAboutMe(); ?>
//html发布于 2013-09-22 04:53:41
可以在包含页面的函数中实例化$db变量,也可以使用global $db;
https://stackoverflow.com/questions/18940258
复制相似问题