[英]PHP passing value to a class
I have a class called DatabaseController which I've set up as so: 我有一个名为DatabaseController的类,它已经这样设置:
<?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 谢谢
In the constructor, $dbName = $passedDBName;
在构造函数中, $dbName = $passedDBName;
should be $this->dbName = $passedDBName;
应该是$this->dbName = $passedDBName;
. 。
Update: 更新:
$this->$dbName
should be $this->dbName
. $this->$dbName
$this->dbName
$this->$dbName
应该是$this->dbName
。 _controller()
should be __construct()
. _controller()
应该是__construct()
。 Use $this->dbName
- otherwise you try to access a field whose name is stored in $dbName
. 使用$this->dbName
否则,您尝试访问名称存储在$dbName
的字段。 You also need to fix your constructur to assign to $this->dbName
instead of $dbName
. 您还需要修复要分配给$this->dbName
而不是$dbName
构造函数。
class DBController {
public $dbName;
function _controller($passedDBName) {
$this->dbName = $passedDBName;
}
function dbConnect() {
print $this->dbName;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.