简体   繁体   English

致命错误:不在对象上下文中时使用$ this-PHP,PDO,会话,

[英]Fatal error: Using $this when not in object context - PHP, PDO, Sessions,

I'm trying to modify a script that stores my session data in a database rather than the default but I'm having a bit of trouble as I keep getting the error above. 我正在尝试修改一个脚本,该脚本将会话数据存储在数据库中而不是默认数据库中,但是由于不断出现上述错误,我遇到了一些麻烦。 It says I'm getting the error on lines 176 and 201. 它说我在第176和201行出现错误。

Here is the function inside the sessionPdo class 这是sessionPdo类中的函数

public function _write($id, $sessionData)
{
    $session = $this->fetchSession($id); // line 176
    if( $session === false ) {
        $stmt = $this->db->prepare('INSERT INTO sessions (id, data, unixtime) VALUES (:id, :data, :time)');
    } else {
        $stmt = $this->db->prepare('UPDATE sessions SET data = :data, unixtime = :time WHERE id = :id');
    }
$stmt->execute( array(
        ':id' => $id,
        ':data' => $sessionData,
        ':time' => time()
    ));

if( $this->transaction ) {
    $this->db->commit();
}
}

And here is the fetchSession method in case it helps. 如果有帮助,这里是fetchSession方法。

public function fetchSession($id)
{
    $stmt = $this->db->prepare('SELECT id, data FROM sessions WHERE id = :id AND unixtime > :unixtime');
    $stmt->execute( array(':id' => $id, ':unixtime' => ( time() - (int) ini_get('session.gc_maxlifetime') ) ) );
    $sessions = $stmt->fetchAll();

    if( $this->transaction ) {
        $this->db->commit();
}

    return empty($sessions) ? false : $sessions[0] ;
}

I've struggled to find what I'm doing wrong as all the solutions I've found are because the programmer tried to call $this from outside of the class. 我一直在努力寻找自己在做错的事情,因为找到的所有解决方案都是因为程序员试图从类外部调用$ this。 I'm new to PDO so thanks in advance for any help you can give. 我是PDO的新手,所以在此先感谢您提供的任何帮助。

You are calling function _write statically, that means somewhere in your code you can find: 您正在静态调用函数_write ,这意味着您可以在代码中的某处找到:

sessionPdo::_write($id, $sessionData);

What you have to do is to create object of sessionPdo class and then run this function on this object, ie: 您要做的是创建sessionPdo类的对象,然后在此对象上运行此函数,即:

$sessObj = new sessionPdo();
$sessObj->_write($id, $sessionData);

Yeah, the problem is that you are calling _write() statically. 是的,问题在于您正在静态调用_write() You need to read up on the difference between static and instance methods. 您需要阅读静态方法和实例方法之间的区别。 I apologize for not explaining it here but there are probably 5,000 people on SO alone who have run into the same problem, which is solved with about 15 minutes of applied reading. 我为在这里没有解释而道歉,但仅SO上就有大约5,000人遇到了相同的问题,而应用阅读大约15分钟即可解决。

When you're comfortable with that, research the Singleton and Factory patterns, which are two ways of providing access to object instances from a static context and are commonly applied in this situation. 如果您对此感到满意,请研究Singleton模式和Factory模式,这是从静态上下文提供对对象实例的访问的两种方式,通常在这种情况下应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM