Serialize / Deserialize / OOP

2019-06-06 01:07发布

问题:

So I was practicing arround with some oop and tried to make a basic oop User class I tried making my system so that on every page I can just include config.php and everthing I need gets loaded in, but for some reason as soon as I try to login it throws me this error :

Fatal error: Call to a member function LogIn() on a non-object

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();
?>

User class :

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>';

So why does it throw out that error ? And why isn't it happening in the index file :S ?>

回答1:

You have to include the class-files BEFORE you start the session, otherwise serialized objects will not be loaded correctly. Unfortunately...



回答2:

If you unserialize an object, which class wasn't yet declared, it gets unserialized into a __PHP_Incomplete_Class, which obviously doesn't have a LogIn method. So you either need to include the file manually, or register an unserialize_callback_func:

ini_set('unserialize_callback_func', '__autoload');


回答3:

Try by removing,

global $user;

Because when your are including file, $user will automatically include in your file.



回答4:

in the config.php file you register a shutdown function that set $_SESSION['User'] with an instance of User (the $user variable)... but because of the way you manage instancied classes (by using global variable) you need to redeclare global $user; in the onExit function.

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

i personnally recommand to have a main global class that handle the instances of the classes you need in your whole application.

edit: look at my answer here



回答5:

Here's a class, which allows to trace changes on global variables:

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 ];

        }

    }

}

Use it like this:

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 output

[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!

While this version CSysTracer tracks changes on globals, you might create a variation of traceGlobalVariable() to trace changes for session variables or e.g. method-calls.