-1
我在嘗試通過OOP方式學習PHP Web開發,並對如何從PHP構造函數方法返回錯誤數組產生疑問。我有一個用戶輸入信息的表單,但是當我檢查他們輸入的數據時,如果他們有錯誤,我希望能夠將其返回到請求它的頁面,以便我可以將它顯示在列表框中在表單上方。我會怎麼做呢?我的代碼如下,並且只是測試代碼,我正在玩PHP來學習我需要在不久的將來構建的網站。謝謝!如何從構造函數方法中返回錯誤PHP
與形式的PHP/HTML文件中它:(別擔心,我也會在工作後發佈表單數據的安全性)
<?php if(!isset($_POST['submit'])) { ?>
**## WANT TO INSERT ERROR LIST HERE IF ANY ERRORS FROM CONSTRUCTOR METHOD ##**
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form1">
Please Enter a First Name: <input type="text" name="fname" /><br />
Please Enter a Last Name: <input type="text" name="lname" /><br />
Please Enter a Username: <input type="text" name="username" /><br />
Please Enter a Email: <input type="text" name="email" /><br />
Please Enter a Password: <input type="password" name="password" /><br />
<input type="submit" value="Submit" name="submit" /><br />
</form>
<?php
}
if (isset($_POST['submit']))
{
include_once("classes/User.class.php");
$u = new User($_POST['fname'],$_POST['lname'],$_POST['username'],$_POST['password'],$_POST['email'],"male");
echo "First Name: " . $u->get_first_name() . "<br />";
echo "Last Name: " . $u->get_last_name() . "<br />";
echo "Username: " . $u->get_username() . "<br />";
echo "Email: " . $u->get_email() . "<br />";
}
?>
以下是創建在那裏我叫新用戶我的類定義我所指的構造方法:
<?php
class User
{
private $firstName = "";
private $lastName = "";
private $username = "";
private $password = "";
private $email = "";
private $gender;
private $birthday;
private $dateJoined;
private $lastVisit;
private $errors = array();
function __construct($fname,$lname,$username,$password,$email,$gender)
{
if($this->check_name($fname,$lname))
{
$this->firstName = $fname;
$this->lastName = $lname;
}
else
{
array_push($this->errors,"Please correct your first and last name");
}
if($this->check_username($username))
{
$this->username = $username;
}
else
{
array_push($this->errors,"Please choose a username.");
}
if ($this->check_password($password))
{
$this->password = sha1($password);
}
else
{
array_push($this->errors,"Passwords must be 7 or more characters");
}
$this->email = $email;
if($gender)
{
$this->gender = $gender;
}
if(!empty($this->errors))
{
foreach ($this->errors as $error) {
echo $error . "<br />";
}
unset($error);
exit();
}
}
//Getter Methods
public function get_first_name()
{
return $this->firstName;
}
public function get_last_name()
{
return $this->lastName;
}
public function get_full_name()
{
return $this->firstName . " " . $this->lastName;
}
public function get_email()
{
return $this->email;
}
public function get_username()
{
return $this->username;
}
public function get_birthday()
{
if ($this->birthday)
return $this->birthday;
}
public function get_gender()
{
if ($this->gender)
return $this->gender;
}
public function get_last_visit()
{
return $this->lastVisit;
}
//Setter Methods
public function set_first_name($fname)
{
$this->firstName = $fname;
}
public function set_last_name($lname)
{
$this->lastName = $lname;
}
public function set_full_name($fname,$lname)
{
$this->firstName = $fname;
$this->lastName = $lname;
}
public function set_email($email)
{
$this->email = $email;
}
public function set_password($password)
{
$this->password = sha1($password);
}
public function set_username($username)
{
$this->username = $username;
}
//Private methods
private function check_name($fname,$lname)
{
if($fname == "" || $lname == "")
return false;
return true;
}
private function check_username($username)
{
if($username == "")
return false;
return true;
}
private function check_password($password)
{
if(!isset($_POST['password']))
return false;
else if (strlen($password) < 7)
return false;
return true;
}
}
?>
這是一個關於設置驗證與用戶類分開的好主意。我不得不查找一些額外的資源,以便我可以正確設置它。感謝那 – vol4life27