php类中变量存储数据问题

php8.0中
比如

class Send
{
    private $_email;
    
    public function sendMail()
    {
        $this->_email->to = 'xxx';
        
        $this->do();
    }

    public function do()
    {
        // code    
    }
}

在此遇到的问题是 使用send类中的 _email 存储数据 不明白 为什么会报错 Attempt to assign property "to" on null;
php 7X中 貌似不会出现类似问题;

阅读 2.8k
1 个回答

PHP: 不向后兼容的变更 - Manual

A number of warnings have been converted into Error exceptions:

Attempting to write to a property of a non-object. Previously this
implicitly created an stdClass object for null, false and empty strings.

即你现在需要在构造函数中声明成一个 stdClass 实例或者其他对象。

包含在这个 RFC 中:PHP: rfc:engine_warnings

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题