1

当我使用此 PHP 编码时,我收到以下错误:

致命错误:无法访问第 29 行 /connection.php 中的空属性

class connectionClass
{

    var $host = '';
    var $user = '';
    var $password = '';
    var $db = '';
    var $con

    function __construct($flag)
    {
        if($flag == "local"):
            $this->host = "localhost";
            $this->user = "root";
        elseif($flag == "remote"):
            $this->host = "192.168.1.2";
            $this->user = "root";
        else:
            echo "Incorrect connection flag.";
        endif;

        $this->password = $password;
        $this->db = $db;
    }

    function connect()
    {
        $con = mysql_connect($this->host, $this->user, $this->password) or die(mysql_error());
        mysql_select_db($this->db, $con) or die(mysql_error());
    }

}

有什么建议么?提前致谢。

4

2 回答 2

4

类中的替代语法是什么?不好的做法——你不是在写模板。使用大括号。

您的问题很可能在这里:

 $this->password = $password;
 $this->db = $db;

您将 $password 分配给您的类变量,但 $password 不是构造函数中的参数。所以你的 $this->password 是空的。$this->db 也有同样的问题。

于 2012-12-22T04:40:44.440 回答
2

问题出在您的“__construct”函数中。

查看第 23 行,'$this->db = $db;'。确保使用值定义局部变量 $db。

注意:变量“$password”(第 22 行)的定义也丢失了。确保你也用一个值来定义它。

于 2012-12-22T04:49:22.443 回答