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.

Here is the function inside the sessionPdo class

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.

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. I'm new to PDO so thanks in advance for any help you can give.

share|improve this question
The code itself looks fine. The problem is probably in whatever called _write() in the first place. If that code calls _write() outside of the object's scope, you'll get this errors as $this won't be defined inside _write() in that case. – Marc B Oct 24 '11 at 18:38
Which lines are 176 and 201? – Jonathan Spooner Oct 24 '11 at 18:42
It has nothing to do with PDO. You aren't by chance calling _write() statically anywhere? sessionPdo::_write(.....)? Try adding print_r(debug_backtrace()) right before line 176 to see what's calling what. – alexantd Oct 24 '11 at 18:42
@Jonathon Spooner - line 176 is commented on the third line of the first section of code. I haven't included line 201 as I figured I'd deal with one problem at a time and hoped that it would be the same solution. – Sean Atkinson Oct 24 '11 at 18:49
@alexantd I've added what you suggested and it has returned this: Array ( [0] => Array ( [function] => _write [class] => we_sessionPdo [type] => :: [args] => Array ( [0] => 2b73efe7a6899da83ab0ad7ccb474560 [1] => access|i:1;currentURL|s:42:"localhost/music-phonebook/index.php";mpbat|i:594546384; ) ) ) which is just some session variables I'm storing as part of the script. – Sean Atkinson Oct 24 '11 at 18:52

2 Answers

Yeah, the problem is that you are calling _write() statically. 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.

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.

share|improve this answer

You are calling function _write statically, that means somewhere in your code you can find:

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:

$sessObj = new sessionPdo();
$sessObj->_write($id, $sessionData);
share|improve this answer
In practice this will work, but it's potentially dangerous. – alexantd Oct 24 '11 at 19:13
Also, if he had full error reporting, he would have seen an earlier warning, eg: Strict Standards: Non-static method sessionPdo::_write() should not be called statically in /code/abc.php on line 999 – Christian Oct 24 '11 at 19:17
I haven't actually used that code, according to the source of the script I'm modifying, the way I actually use $_SESSION variables shouldn't change and all I needed to do was include the script, however, when I' have I've been faced with these errors. I'll investigate and add more details if I can find what you're talking about. – Sean Atkinson Oct 24 '11 at 19:43
Though I've already including the first part of your suggestion creating an instance of the object. I was having a simliar problem at an earlier stage where I couldn't link to my database. I solved this by creating the instance of the PDO and passing it as a variable to my controller and on to my model. Will I also need to do the same with my $session PDO? – Sean Atkinson Oct 24 '11 at 19:50
What I know for sure is that you need to read something about OOP in PHP (and OOP in general), try some examples, get accustomed to Singelton/Factory/Dependency Injection/Observer pattern and then decide what fits best into your application. – dev-null-dweller Oct 24 '11 at 20:26

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.