PHP会话和类成员

时间:2010-12-25 23:40:00

标签: php oop session

好的,搞乱PHP中的类,无法像以前那样以C ++ / Java人的方式工作。在“_init”函数中,如果我在“// query works here”行运行一个查询,“everythong有效,但在”getUserID“函数中,所有发生的事情都是警告...

“getUserID”从login.php调用(它们在同一个目录中):

的login.php

<?php
include_once 'sitehandler.php';
include_once 'dbhandler.php';
session_start();

#TODO: Safer input handling
$t_userName = $_POST["name"];
$t_userId = $_SESSION['handler']['db']->getUserID($t_userName);

if ($t_userId != -1) {
    $_SESSION['user']['name'] = $t_userName;
    $_SESSION['user']['id'] = $t_userId;
}

//error_log("user: " . $_SESSION['user']['name'] . ", id: ". $_SESSION['user']['id']);
header("Location: " . $_SERVER["HTTP_REFERER"]);
?>

dbhandler.php

 <?php
include_once 'handler.php'; 

class DBHandler extends HandlerAbstract {
private $m_handle;

function __construct() {
    parent::__construct();
}

public function test() {
    #TODO: isdir liquibase
    #TODO: isfile liquibase-195/liquibase + .bat + execrights
    $this->m_isTested = true;
}

public function _init() {
    if (!$this->isTested()) $this->test();
    if (!file_exists('files/data.db')) {
        #TODO: How to to if host is Windows based?
        exec('./files/liquibase-1.9.5/liquibase --driver=org.sqlite.JDBC --changeLogFile=files/data_db.xml --url=jdbc:sqlite:files/data.db update');
        #TODO: quit if not success
    }

    #TODO: Set with default data
    try {
        $this->m_handle = new SQLite3('files/data.db');
    } catch (Exception $e) {
        die("<hr />" . $e->getMessage() . "<hr />");
    }

    // query works here
    $this->m_isSetup = true;
}

public function teardown() {

}

public function getUserID($name) {
    //  PHP Warning:  SQLite3::prepare(): The SQLite3 object has not been correctly initialised in
    $t_statement = $this->m_handle->prepare("SELECT id FROM users WHERE name = :name");
    $t_statement->bindValue(":name", $name, SQLITE3_TEXT);
    $t_result = $t_statement->execute();
        //var_dump($this->m_handle);

        return ($t_result)? (int)$t_result['id']: -1;
    }
}

1 个答案:

答案 0 :(得分:1)

当您在会话中放置内容并且脚本结束时,PHP会遍历每个对象并调用__sleep()魔术函数。一旦恢复会话,就会调用__wakeup()

资源不存储在会话中。因此,每次您希望拥有可用资源时,必须在每次运行脚本时对其进行初始化。在您的示例中,可以通过实施__wakeup()魔术方法轻松完成此操作,该方法应调用$this->_init();

您的代码可能会变为:

<?php

include_once 'handler.php';

class DBHandler extends HandlerAbstract { private $m_handle;

    function __construct() {
        parent::__construct();
    }

    public function test() {
        #TODO: isdir liquibase
        #TODO: isfile liquibase-195/liquibase + .bat + execrights
        $this->m_isTested = true;
    }

    public function _init() {
        if (!$this->isTested()) $this->test();
        if (!file_exists('files/data.db')) {
            #TODO: How to to if host is Windows based?
            exec('./files/liquibase-1.9.5/liquibase --driver=org.sqlite.JDBC --changeLogFile=files/data_db.xml --url=jdbc:sqlite:files/data.db update');
            #TODO: quit if not success
        }

        #TODO: Set with default data
        try {
            $this->m_handle = new SQLite3('files/data.db');
        } catch (Exception $e) {
            die("<hr />" . $e->getMessage() . "<hr />");
        }

        // query works here
        $this->m_isSetup = true;
    }

    public function teardown() {

    }

    public function getUserID($name) {
        //  PHP Warning:  SQLite3::prepare(): The SQLite3 object has not been correctly initialised in
        $t_statement = $this->m_handle->prepare("SELECT id FROM users WHERE name = :name");
        $t_statement->bindValue(":name", $name, SQLITE3_TEXT);
        $t_result = $t_statement->execute();
        //var_dump($this->m_handle);

        return ($t_result)? (int)$t_result['id']: -1;
    }

    /**
    * Start up the SQLite resource once
    * the session is started.
    * 
    */
    public function __wakeup() {
        $this->init();
    }
}