所以我正在练习一些oop的arround并试图做一个基本的oop用户类我试图让我的系统,以便在每一页上我可以只包括config.php和everthing我需要被加载,但由于某些原因,当我尝试登录它抛出我这个错误:序列化/反序列化/ OOP
致命错误:调用一个成员函数的login()一个非对象
的config.php:
<?php
session_start();
// Mysql details
$username = "torlolol";
$password = "torlolol";
$hostname = "localhost";
$database = "torlolol";
// Autoloads classes when needed
function __autoload($class_name) {
include 'Classes/' . $class_name . '.php';
}
$db = new DBConnector($hostname, $username, $password);
// If there is no user in the session stored make a new one otherwise unserialize it
if(empty($_SESSION['user']))
$user = new User();
else
$user = unserialize($_SESSION['user']);
function onExit()
{
//When exiting the page serialize the user object and store it in the session
$_SESSION['user'] = serialize($user);
}
register_shutdown_function("onExit")
?>
login.php
<?php
include "config.php";
global $user;
$user->LogIn();
?>
用户等级:
class User {
public $Logged = false;
public function LogIn()
{
$this->Logged = true;
}
public function LogOut()
{
$this->Logged = false;
}
}
?>
的index.php:
<?php
include "config.php";
global $user;
if ($user->Logged != true)
echo '<form action="login.php" method="post">Username :<input type="text" name="username" /> Password :<input type="password" name="password" /> <input type="submit" value="Login" /></form>';
else
echo '<form action="logout.php" method="post"><input style="submit" value="Logout" /></form>';
那么,为什么它抛出的错误?为什么它不在索引文件中发生:S ?>
如果你直接在你的index.php中包含用户类是否有效?你的httpd日志文件中有任何错误? – sdolgy 2011-04-22 12:41:38
您是否尝试使用'var_dump($ user);''来查看'$ user'变量中的实际内容?你是否已经检查了在'config.php'中创建'$ user'实例的所有步骤? – zerkms 2011-04-22 12:43:36
调试你的代码,你会看到有什么问题!我的意思是一个调试器! – markus 2011-04-22 12:48:23