【问题标题】:php class attribute inizialized in the cunstroctor not retaining value在构造函数中初始化的php类属性不保留值
【发布时间】:2021-11-16 16:04:55
【问题描述】:

我正在尝试将类添加到项目中,在构造函数中初始化类属性后,该类似乎没有保留该值。

如果我在方法中强制属性值,我得到我想要的。我错过了什么吗?

class test {
    public $test_var;

    public function __constructor(){
        $this->test_var = 'hello';
    }

    public function add_record(){
//        $this->test_var = 'hello';

        print $this->test_var;
    }
}

$test = new test();
$test->add_record();

【问题讨论】:

    标签: php class methods


    【解决方案1】:

    __constructor 不是你想要的。应该是

     public function __construct(){
    

    【讨论】:

    • 你称它为 __contructor 但正确的语法是 __construct(最后不带“或”)
    猜你喜欢
    • 2019-07-31
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多