在自定义会话功能中使用闭包

时间:2013-06-17 01:37:45

标签: php session closures

我正在尝试为PHP编写一个自定义SESSION类,以便将会话移动到数据库中并远离文件系统。 在调用闭包_read()时,仅在$connect2DB方法中抛出错误。在调用$connect2DB的所有其他方法中,不会导致任何错误。 此外,如果我将类作为对象实例化,那么包括臭名昭着的_read()在内的所有方法都会按预期工作。你能找到错误吗?

我有以下代码:

class sessionDBHandler {
    public function __construct() {
        session_set_save_handler(
            array($this, "_open"),
            array($this, "_close"),
            array($this, "_read"),
            array($this, "_write"),
            array($this, "_destroy"),
            array($this, "_gc")
        );
        register_shutdown_function('session_write_close');
    }

    public function _open() {
        global $sessCon;
        global $HOST;
        global $USER;
        global $PW;
        global $DATABASE;

        $sessCon=new mysqli($HOST,$USER,$PW,$DATABASE); //mysqli connection
    }

    public function _close() {
        global $sessCon;

        $sessCon->close();
    }

    public function _read($id) {
        global $sessCon;
        global $connect2DB;
        global $SESSIONTAB;

        $qRes=$connect2DB($sessCon,'select `data` from `'.$SESSIONTAB.'` where `ID`=?',array("s",$id)); //<-- error message here

        if(!empty($qRes["res"][0]["data"])) return $qRes["res"][0]["data"];
        else return '';
    }

    public function _write($id,$data) {
        global $sessCon;
        global $connect2DB;
        global $SESSIONTAB;

        $qRes=$connect2DB($sessCon,'select `ID` from `'.$SESSIONTAB.'` where `ID`=?',array("s",$id));

        $param=array(); //buliding SQL query parameters
        $param[0]="sis";
        $param[]=$id;
        $param[]=time();
        $param[]=$data;
        if(!empty($qRes["res"][0]["ID"])) {
            $param[0].="s";
            $param[]=$id;
        }

        $qRes=$connect2DB($sessCon,(!empty($qRes["res"][0]["ID"]) ? 'update' : 'insert').' `'.$SESSIONTAB.'` set `ID`=?, `access`=?, `data`=?'.(!empty($qRes["res"][0]["ID"]) ? ' where `ID`=?' : ''),$param);

        if($qRes["info"]["affectedRows"]>0) return true;
        else return '';
    }

    public function _destroy($id) {
        global $sessCon;
        global $connect2DB;
        global $SESSIONTAB;

        $qRes=$connect2DB($sessCon,'delete from `'.$SESSIONTAB.'` where `ID`=?',array("s",$id));

        if($qRes["info"]["affectedRows"]>0) return true;
        else return '';
    }

    public function _gc($max) {
        global $sessCon;
        global $connect2DB;
        global $SESSIONTAB;

        $qRes=$connect2DB($sessCon,'delete from `'.$SESSIONTAB.'` where `access`<?',array("i",(time()-$max)));

        if($qRes["info"]["affectedRows"]>0) return true;
        else return '';
    }
}

我告诉PHP它在哪里完成这些功能(在调用session_start()之前:

new sessionDBHandler();

我的问题似乎是我用作MySQL抽象层的闭包。我得到的错误是:

Fatal error: Function name must be a string in /Users/user/Sites/script.php on line 401

这告诉我,它以某种方式无法找到$ connect2DB的命名空间。 但事情是,如果我在没有__constructor及其session_set_save_handler指令的情况下调用这些函数,它将正常工作:

$test=new sessionDBHandler();
$test->_open();
echo $test->_read("d6e08112ff6fc8a7eb6e76832327bf81");
$test->_close();

以上工作没有错误。 现在我在这里使用闭包因为我必须保持这个脚本的足迹非常低并且在脚本底部取消设置$ connect2DB以及其他原因所以我希望保留当前的抽象层。

我能在这做什么想法?

谢谢大家!

在调用会话时,可能没有初始化$ connect2DB吗?

connnect2DB

/**
* Simple MySQLi abstraction layer
*
* @param resource $mysqli The MySQLi connection link
* @param string   $query  The MySQL query for prepaired statement
* @param array    $v      The parameters to replace ? in $query. First element must be the type
* @param integer  $o      Option for more debug infos [0]=no infos(default) [1]=adding debug infos
*
* @return array [for select]=associative array of table result  [for everything else]=associative array with affectedRows,info and insertID
*
* @author Dominik Wilkowski
*/
$connect2DB=function($mysqli,$query,$v=array(),$o=0) {

    if($mysqli->connect_errno) {
        return array('info'=>array('error'=>'Connect failed: '.$mysqli->connect_error)); //error handling here
        exit();
    }

    if($v && (substr_count($query,"?")!=strlen($v[0]) || strlen($v[0])!=((count($v)-1)>=0 ? (count($v)-1) : 0))) {
        return array('info'=>array('error'=>'Placeholders are unequal! placeholders:'.substr_count($query,"?").', replacements:'.strlen($v[0]).', param:'.(count($v)-1).' ('.$v[0].')')); //error handling here...
        exit();
    }

    if($res=$mysqli->prepare($query)) {
            //dynamically bind all $v
            if($v) {
            $values=array($v[0]);
            for($i=1; $i<count($v); $i++) {
                ${'bind'.$i}=$v[$i];
                $values[]=&${'bind'.$i};
            }
            call_user_func_array(array($res,'bind_param'),$values);
        }
        $res->execute();

        //bind all table rows to result
        if(strtolower(substr($query,0,6))=="select") {
            $field=$fields=$tempRow=array();
            $meta=$res->result_metadata();
            while($field=$meta->fetch_field()) {
                $fieldName=$field->name;
                $fields[]=&$tempRow[$fieldName];
            }
            $meta->free_result();
            call_user_func_array(array($res,"bind_result"),$fields);

            //return associative array
            $results=array();
            $i=0;
            while($res->fetch()) {
                $results["res"][$i]=array();
                foreach($tempRow as $k=>$v2) $results["res"][$i][$k] = $v2;
                $i++;
            }
            $res->free_result();

        }
        else { //return infos about the query

            if($mysqli->warning_count) {
                if($err=$mysqli->query("SHOW WARNINGS")) {
                    $row=$err->fetch_row();
                    $results["info"]["error"].=$row[0].' ('.$row[1].'): '.$row[2];
                    $err->close();
                }
            }
            $results["info"]["affectedRows"]=$mysqli->affected_rows;
            $results["info"]["info"]=$mysqli->info;
            $results["info"]["insertID"]=$mysqli->insert_id;
        }

        $res->close();
    }

    if($o===1) { //adding debug infos
        $q=$query;
        for($i=1;$i<count($v);$i++) $q=preg_replace("/\?/",(substr($v[0],($i-1),1)=="s" ? '"' : '').$v[$i].(substr($v[0],($i-1),1)=="s" ? '"' : ''),$q,1);
        $results["info"]["query"]=$q;
        $results["info"]["param"]=json_encode($v);
    }

    if(strtolower(substr($query,0,6))=="update" || strtolower(substr($query,0,6))=="delete") { //optimize at update and delete
        preg_match_all('/((update|delete) `(.*)` )/i',$query,$tables);
        foreach($tables[3] as $t) $mysqli->query('OPTIMIZE TABLE '.$t);
    }

    return $results;
};

更新#2

我们之前的测试表明,在_read()运行时,命名空间确实似乎没有被初始化,所以我写了一个小测试用例来进行双重检查。

<?php
$LOGIN='login credentials';

//Closure
$nob=function() {
    return 'closure running!<br>';
};

//Session handler
class sessionDBHandler {
    public function __construct() {
        session_set_save_handler(
            array($this, "_open"),
            array($this, "_close"),
            array($this, "_read"),
            array($this, "_write"),
            array($this, "_destroy"),
            array($this, "_gc")
        );
        register_shutdown_function('session_write_close');
    }

    public function _open() {
        global $sessCon;
        global $LOGIN;

        echo '_open running with: '.$LOGIN.'<br>';
        $sessCon='connection established!<br>';
    }

    public function _close() {
        global $sessCon;

        echo $sessCon='connection closed!<br>';
    }

    public function _read($id) {
        global $sessCon;
        global $nob;

        echo $nob();
        echo 'requested id is: '.$id.'<br>';
        echo 'sessCon is: '.$sessCon;
    }

    public function _write($id,$data) {
        global $sessCon;

        echo 'requested id is: '.$id.' and data: '.$data.'<br>';
        echo 'sessCon is: '.$sessCon;
    }

    public function _destroy($id) {
        global $sessCon;

        echo 'requested id is: '.$id.'<br>';
        echo 'sessCon is: '.$sessCon;
    }

    public function _gc($max) {
        global $sessCon;

        echo 'requested max is: '.$max.'<br>';
        echo 'sessCon is: '.$sessCon;
    }
}

new sessionDBHandler();
session_start();
?>

有趣的是,这将运行没有错误并输出:

_open running with: login credentials
closure running!
requested id is: 746b70a979d9ef8c6686e659707b4b38
sessCon is: connection established!
requested id is: 746b70a979d9ef8c6686e659707b4b38 and data:
sessCon is: connection established!
connection closed!

现在这表明我的特定实现中似乎存在错误。无论谁想要这样做都非常欢迎加入:)

更新#3

我想在运行我的应用程序之后取消设置我的闭包,因此在脚本的最底部有unset($connect2DB);。然而,这似乎导致了某种“过早”的不成熟。删除它将删除错误。

然而

有没有办法在脚本运行后安全地取消设置我的闭包,因此它不会流入主脚本?

0 个答案:

没有答案