Reputation: 6384
I have a class called DatabaseController which I've set up as so:
<?php
class DBController {
public $dbName;
function _controller($passedDBName) {
$dbName = $passedDBName;
}
function dbConnect() {
print $this->$dbName;
}
} //end of class
?>
and I am calling it this way:
<?php
//make new database connection
$dbManager = new DBController("somevalue");
$dbManager->dbConnect();
?>
but I keep getting this error:
<b>Fatal error</b>: Cannot access empty property in
What am I doing wrong?
Thanks
Upvotes: 0
Views: 76
Reputation: 16462
In the constructor, $dbName = $passedDBName;
should be $this->dbName = $passedDBName;
.
Update:
$this->$dbName
should be $this->dbName
._controller()
should be __construct()
.Upvotes: 3
Reputation: 318728
Use $this->dbName
- otherwise you try to access a field whose name is stored in $dbName
. You also need to fix your constructur to assign to $this->dbName
instead of $dbName
.
class DBController {
public $dbName;
function _controller($passedDBName) {
$this->dbName = $passedDBName;
}
function dbConnect() {
print $this->dbName;
}
}
Upvotes: 3