代码之家  ›  专栏  ›  技术社区  ›  Hugo Mota

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

  •  0
  • Hugo Mota  · 技术社区  · 15 年前

    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
    

    功能是:

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

    奇怪的是:当我将类更改为不再扩展PDO,而是将其包含在一个变量中时,错误就消失了。

    这很好:

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

    为什么?

    1 回复  |  直到 15 年前
        1
  •  6
  •   ircmaxell    15 年前

    答案很简单。你的代码错了。它在做无限递归。

    $this->commit() ,你调用的是同一个方法。所以它会一直循环,直到内存耗尽,或者堆栈溢出(点击StackOverflow,呵呵)。

    相反,将函数更改为调用父类的 commit() PDO::commit() ):

    function commit() {
        return parent::commit();
    }
    
    推荐文章