Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP会话和类成员_Php_Oop_Session - Fatal编程技术网

PHP会话和类成员

PHP会话和类成员,php,oop,session,Php,Oop,Session,好吧,在PHP中乱搞类,不能让它像我作为一个C++/Java的家伙那样工作。在_init函数中,如果我在//query works here行运行一个查询,一切都可以运行,但是在getUserID函数中,所有发生的事情都是警告 getUserID从login.php调用,它们位于同一目录中: login.php dbhandler.php 当您在会话中放置一些内容并且脚本结束时,PHP将遍历每个对象并调用_sleep magic函数。一旦它恢复会话,就会调用唤醒 资源不存储在会话中。因此,每当您

好吧,在PHP中乱搞类,不能让它像我作为一个C++/Java的家伙那样工作。在_init函数中,如果我在//query works here行运行一个查询,一切都可以运行,但是在getUserID函数中,所有发生的事情都是警告

getUserID从login.php调用,它们位于同一目录中:

login.php

dbhandler.php


当您在会话中放置一些内容并且脚本结束时,PHP将遍历每个对象并调用_sleep magic函数。一旦它恢复会话,就会调用唤醒

资源不存储在会话中。因此,每当您希望资源可用时,必须在每次脚本运行时对其进行初始化。在您的示例中,这可以通过实现uu wakeup magic方法轻松实现,该方法应调用$This->u 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();
    }
}

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