为什么扩展PDO会导致内存溢出?

Hug*_*ota 0 php database pdo class

我有一个扩展PDO类的类.它叫做数据库.但在一个特定的函数,commit()函数,它会得到一个内存溢出错误.

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
Run Code Online (Sandbox Code Playgroud)

功能是:

function commit() {
    return $this->commit();
}
Run Code Online (Sandbox Code Playgroud)

好奇的是:当我改变我的类不再扩展PDO,但只是将它包含在一个变量中时,错误消失了.

这很好用:

function commit() {
    return $this->pdo->commit();
}
Run Code Online (Sandbox Code Playgroud)

这是为什么?

irc*_*ell 6

答案很简单.你的代码错了.它正在进行无限递归.

当你打电话时$this->commit(),你正在调用同样的方法.所以它将永远循环,直到你的内存耗尽,或者你溢出堆栈(点击StackOverflow,hehehe).

相反,更改函数以调用父类的commit()方法(在本例中为:)PDO::commit():

function commit() {
    return parent::commit();
}
Run Code Online (Sandbox Code Playgroud)