views:

33

answers:

1

i have a class that extends the PDO class. it's called Database. but in a particular function, the commit() function, it gets an memory overflow error.

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261900 bytes) in C:\wamp\www\igadgets\application\includes\base\classes\database.php on line 130

the function was:

function commit() {
    return $this->commit();
}

the curious is: when i change my class to not extend PDO anymore, but just contain it in a variable, the error disappears.

this works fine:

function commit() {
    return $this->pdo->commit();
}

why is that?

+5  A: 

The answer is simple. Your code is wrong. It's doing infinite recursion.

When you call $this->commit(), you're calling that very same method. So it will just loop on forever until either you run out of memory, or you overflow the stack (hit a StackOverflow, hehehe).

Instead, change the function to call the parent class's commit() method (which in this case is: PDO::commit()):

function commit() {
    return parent::commit();
}
ircmaxell
wow! didn't even noticed that! thanks ^^
hugo_leonardo
+1 for terseness. "Your code is wrong."
webbiedave