开发者

why does extending PDO causes me a memory overflow?

开发者 https://www.devze.com 2023-01-17 07:58 出处:网络
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.

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?


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

精彩评论

暂无评论...
验证码 换一张
取 消