序列化/反序列化/ OOP

时间:2011-04-22 12:39:15

标签: php oop session serialization

所以我练习了一些oop并尝试制作一个基本的oop用户类我尝试制作我的系统,以便在每个页面上我可以只包括config.php和everthing我需要加载,但由于某种原因,一旦我尝试登录它就会抛出这个错误:

致命错误:在非对象上调用成员函数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" />&nbsp;&nbsp;<input type="submit" value="Login" /></form>';
                else    
                    echo '<form action="logout.php" method="post"><input style="submit" value="Logout" /></form>';

那为什么会抛出这个错误呢?为什么不在索引文件中发生:S                 ?&GT;

5 个答案:

答案 0 :(得分:3)

您必须在启动会话之前包含类文件,否则将无法正确加载序列化对象。不幸的是...

答案 1 :(得分:2)

如果你反序列化一个对象,哪个类尚未声明,它将被反序列化为__PHP_Incomplete_Class,显然没有LogIn方法。因此,您需要手动包含文件,或者注册unserialize_callback_func

ini_set('unserialize_callback_func', '__autoload');

答案 2 :(得分:1)

尝试删除,

global $user;

因为当你包含文件时,$ user会自动包含在你的文件中。

答案 3 :(得分:0)

在config.php文件中注册了一个关闭函数,该函数将$ _SESSION ['User']设置为User($ user变量)...但是由于您管理实例化类的方式(通过使用全局)变量)您需要在global $user;函数中重新声明onExit

function onExit()
{
    //When exiting the page serialize the user object and store it  in the session
    global $user;
    $_SESSION['user'] = serialize($user);
}

我个人建议有一个主要的全局类来处理整个应用程序中所需的类的实例。

编辑:查看我的回答here

答案 4 :(得分:0)

这是一个类,它允许跟踪全局变量的变化:

abstract class CSTReportDelegate {

    abstract public function emitVariableChange( $variableName, $oldValue, $newValue );
    abstract public function emitVariableSetNew( $variableName, $newValue );

}

class CSTSimpleReportDelegate extends CSTReportDelegate {

    public function emitVariableChange( $variableName, $oldValue, $newValue ) {
        echo '<br />[global/change] '. $variableName . ' : ' . print_r( $oldValue, true ) . ' &rarr; ' . print_r( $newValue, true );
    }

    public function emitVariableSetNew( $variableName, $newValue ) {
        echo '<br />[global/init] '. $variableName . '   &rarr; ' . print_r( $newValue, TRUE );
    }

}


class CSysTracer {

    static protected 
        $reportDelegate;

    static private 
        $globalState = array();

    static private  
        $traceableGlobals = array();

    static private 
        $globalTraceEnabled = FALSE;

    static public 
    function setReportDelegate( CSTReportDelegate $aDelegate ) {
        self::$reportDelegate = $aDelegate;
    }


    static public 
    function start( ) {

        register_tick_function ( array( 'CSysTracer', 'handleTick' ) );

    }


    static public 
    function stop() {

        unregister_tick_function( array( 'CSysTracer', 'handleTick' ) );

    }

    static public 
    function evalAndTrace( $someStatement ) {

        declare( ticks = 1 ); {
            self::start();
            eval( $someStatement );
            self::stop();
        }
    }

    static public 
    function addTraceableGlobal( $varName ) {

        if ( is_array( $varName )) {
            foreach( $varName as $singleName ) {
                self::addTraceableGlobal( $singleName ); 
            }
            return;
        }

        self::$traceableGlobals[ $varName ] = $varName;

    }

    static public 
    function removeTraceableGlobal( $varName ) {
        unset( self::$traceableGlobals[ $varName ] );   
    }

    /**
     * Main function called at each tick. Calls those functions, which
     * really perform the checks.
     * 
     */
    static public 
    function handleTick( ) {

        if ( TRUE === self::$globalTraceEnabled ) { 
            self::traceGlobalVariable();
        }

    }

    static public 
    function enableGlobalsTrace() {
        self::$globalTraceEnabled = TRUE;   
    }


    static public 
    function disableGlobalsTrace() {
        self::$globalTraceEnabled = FALSE;  
    }

    static public 
    function traceGlobalVariable( ) {

        foreach( self::$traceableGlobals as $aVarname ) {

            if ( ! isset( $GLOBALS[ $aVarname ] )) {
                continue;
            }

            if ( ! isset( self::$globalState[ $aVarname ] ) ) {

                self::$reportDelegate->emitVariableSetNew( $aVarname, $GLOBALS[ $aVarname ] );
                self::$globalState[ $aVarname ] = $GLOBALS[ $aVarname ];
                continue;
            }

           if ( self::$globalState[ $aVarname ] !== $GLOBALS[ $aVarname ]) {

             self::$reportDelegate->emitVariableChange( $aVarname, self::$globalState[ $aVarname ], $GLOBALS[ $aVarname ] );

           }

           self::$globalState[ $aVarname ] = $GLOBALS[ $aVarname ];

        }

    }

}

像这样使用:

CSysTracer::addTraceableGlobal( array( 'foo', 'bar' ));

CSysTracer::setReportDelegate( new CSTSimpleReportDelegate() ); 
CSysTracer::enableGlobalsTrace();

CSysTracer::start(); 
declare( ticks = 1 );

    $foo = 10;
    echo "<br>First output";
    $foo *= $foo;
    $foo = 'bar';
       echo "<br>Next output";
    $otherFoo = array (1,2,3);
    $bar = 23;
    echo "<br>Finished!";

CSysTracer::stop();

Whill输出

[global/init] foo → 10
Hi!
[global/change] foo : 10 → 100
[global/change] foo : 100 → bar
Gotta run
[global/change] foo : bar → Array ( [0] => 1 [1] => 2 [2] => 3 ) 
[global/init] bar → 23
Bye!

虽然此版本CSysTracer跟踪全局变量,但您可以创建traceGlobalVariable()的变体来跟踪会话变量的变化,例如方法的呼叫。