duan20145 2017-10-25 09:42
浏览 99

PHP无法找到私有变量[关闭]

For some reason PHP can't find my private variables.

I get following error message

*6 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined variable: _database in /var/www/simple-blog/models/database.php on line 24 PHP message: PHP Fatal error: Uncaught Error: Cannot access empty property in /var/www/simple-blog/models/database.php:24

Line 24 is the line where I set $_database = $database.

class Database {

private $_database;
private $_mysqli;


public function connect(string $host, string $username, string $password, string $database) : bool
{
    $this->$_database = $database;

    $this->$_mysqli = new mysqli($host, $username, $password, $database);

    if ($this->$_mysqli->connect_error) {
        return false;
    } else {
        return true;
    }
}
...

I tried removing that aready, but then PHP can't find the variable $_mysqli

  • 写回答

3条回答 默认 最新

  • du521521521 2017-10-25 09:44
    关注

    The way you used the variable is not correct. For more detail on php class variable have a look at http://php.net/manual/en/language.oop5.visibility.php

    Change $this->$_database to $this->_database. Change $this->$_mysql to $this->_mysql

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料