0

我有一个名为 DatabaseController 的类,我已将其设置为:

<?php

class DBController {

    public $dbName;

    function _controller($passedDBName) { 
        $dbName = $passedDBName;
    }

    function dbConnect() { 
        print $this->$dbName;
    }



} //end of class       
?>

我这样称呼它:

<?php

    //make new database connection
    $dbManager = new DBController("somevalue");
    $dbManager->dbConnect();


?>

但我不断收到此错误:

<b>Fatal error</b>:  Cannot access empty property in 

我究竟做错了什么?

谢谢

4

2 回答 2

3

在构造函数中,$dbName = $passedDBName;应该是$this->dbName = $passedDBName;.

更新:

  1. $this->$dbName应该是$this->dbName
  2. _controller()应该是__construct()
于 2012-05-26T17:23:35.227 回答
3

使用$this->dbName- 否则您将尝试访问名称存储在$dbName. 您还需要修复您的构造函数以分配给$this->dbName而不是$dbName.

class DBController {
    public $dbName;
    function _controller($passedDBName) { 
        $this->dbName = $passedDBName;
    }
    function dbConnect() { 
        print $this->dbName;
    }
}
于 2012-05-26T17:23:36.990 回答