diffnest / myblog

0 stars 0 forks source link

一段PHP代码引发一段(Segmentation fault)错误??? #22

Open diffnest opened 6 years ago

diffnest commented 6 years ago

php报(Segmentation fault)错误,经过断点跟踪,代码自身调用了自身构造,导致死循环,栈溢出了;

class jrrecharge
{
    function preDonePayInterest($borrowerUid, $loanId, $step, $saleNo) {
        return $this->mod('jrrecharge')->preDonePayInterest($borrowerUid, $loanId, $step, $saleNo);
}

简单测试:

<?php 
class classA 
{
    public function __construct() {
        echo "classA __construct". PHP_EOL;
    }
}

class classB extends classA 
{
    public function __construct() {
        echo "classB __construct". PHP_EOL;
        self::__construct();
    }
}

$a = new classB();

结果输出:

classB __construct
classB __construct
classB __construct
classB __construct
classB __construct
classB __construct
classB __construct
classB __construct
classB __construct
classB __construct
classB __construct
classB __construct
classB __construct
classB __construct
classB __construct
···························
//本地环境ERROR:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in /Users/jingwei/yc.php on line 14
//LINUX服务器ERROR:
Segmentation fault (core dumped)

原因很简单:
调用了自身construct,进入死循环了,应该是parrent::construct();而不是self::construct().

造成栈溢出可能还有其他情况,比如超巨大局部变量,函数无穷递归~