__construct是在PHP5中引入的,它是定义您的构造函数的正确方法(在PHP4中,您将类的名称用作构造函数)。您无需在类中定义构造函数,但是如果希望在对象构造上传递任何参数,则需要一个。
一个例子可能是这样的:
class Database { protected $userName; protected $password; protected $dbName; public function __construct ( $UserName, $Password, $DbName ) { $this->userName = $UserName; $this->password = $Password; $this->dbName = $DbName; }}// and you would use this as:$db = new Database ( 'user_name', 'password', 'database_name' );PHP手册中介绍了其他所有内容:单击此处



