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

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

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

简单测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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();

结果输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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().
造成栈溢出可能还有其他情况,比如超巨大局部变量,函数无穷递归~

-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!