gpt4 book ai didi

php - 扩展抽象类时的意外结果

转载 作者:行者123 更新时间:2023-12-02 22:13:20 24 4
gpt4 key购买 nike

我试图故意打印错误“出现错误”,但收到的却是“成功”消息?

知道为什么吗?

谢谢

abstract class Restful
{
public $error = array();

public function __construct()
{
//Doing something here
//....
//....
$this->validate_params();
}

public function validate_params()
{
$this->error[] = 'test error';
}
}

class RestfulRequest extends Restful
{
public function __construct()
{
if (count($this->error) > 0)
{
exit('There is an error');
}

echo 'Success';
}
}

new RestfulRequest();

最佳答案

您忘记调用父构造函数:

class RestfulRequest extends Restful
{
public function __construct()
{
parent::__construct(); // <-- added
if (count($this->error) > 0)
{
exit('There is an error');
}

关于php - 扩展抽象类时的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14894279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com