从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']);