所以,你宣佈你variables
第一,這將輸出初始化它們在__construct
方法
$DB_USER = 'foo';
class Database
{
private $DB_USER,
$DB_PASS,
$DB_DRIVER;
public function __construct(){
$this->DB_USER = $GLOBALS['DB_USER'];
$this->DB_PASS = 'foob';
$this->DB_DRIVER = 'foob_foob';
print_r($this);
}
}
new Database();
Database Object
(
[DB_USER:Database:private] => foo
[DB_PASS:Database:private] => foob
[DB_DRIVER:Database:private] => foob_foob
)
DEMO
編輯
使用依賴注入:
$DB_USER = 'foo';
class Database
{
private $DB_USER,
$DB_PASS,
$DB_DRIVER;
public function __construct($dbUser){
$this->DB_USER = $dbUser;
$this->DB_PASS = 'foob';
$this->DB_DRIVER = 'foob_foob';
print_r($this);
}
}
new Database($DB_USER);
DEMO_2
嘗試'專用$ DB_USER = $ DB_USER',但'$ GLOBALS'是邪惡 – Andrew