我已經__construct($參數)如何使用可選參數創建構造函數?
public function __construct($nick) {
$query = "SELECT * FROM Users WHERE nick='".$nick."'";
$result = App::runQuery($query);
$user = $result->fetch_object();
$this->id = $user->id;
$this->nick = $user->nick;
$this->email = $user->email;
$this->password = $user->password;
$this->birthDay = $user->birthDay;
$this->sex = $user->sex;
$this->about = $user->about;
$this->city = $user->city;
$this->photo = $user->photo;
$this->following = $user->following;
$this->followers = $user->followers;
$this->statu = $user->statu;
}
現在我想有這樣的無參數的默認構造函數:
public function __construct() {
$this->id = NULL;
$this->nick = NULL;
$this->email = NULL;
$this->password = NULL;
$this->birthDay = NULL;
$this->sex = NULL;
$this->about = NULL;
$this->city = NULL;
$this->photo = NULL;
$this->following = NULL;
$this->followers = NULL;
$this->statu = NULL;
}
但什麼是錯的,我得到的錯誤是這樣的:
Fatal error: Cannot redeclare User::__construct() in /Applications/MAMP/htdocs/xxx/Application/User.php on line 33
我怎樣纔能有兩個參數和不帶參數的構造函數在同一時間?
雖然你可能在這兩年後沒有工作,但你在這段代碼中有一個明顯的SQL注入漏洞 – Casey 2017-07-27 19:23:41