從Properties手冊頁:
Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
會話數據不適合在編譯時類別,這樣你的屬性只能與普通的代碼進行設置,或者在類外(因爲他們是公共的)或來自方法(可能是構造函數)。
這通常不實際的硬編碼這些值到一個類代碼,所以我會去的構造方法:
<?php
class pardEngine{
private $hostname;
private $database;
private $database_user;
private $database_user_pass;
public function __construct($hostname, $database, $database_user, $database_user_pass){
$this->hostname = $hostname;
$this->database = $database;
$this->database_user = $database_user;
$this->database_user_pass = $database_user_pass;
}
}
session_start();
$engine = new pardEngine($_SESSION['HOSTNAME'], $_SESSION['DATABASE'], $_SESSION['USER'], $_SESSION['PASSWORD']);