2014-11-08 63 views
-2

我有一個php表單類,用於驗證表單輸入。但是,在實現isEmailEmpty()函數時,我遇到了一些意想不到的行爲。這裏是我的代碼:php bool函數返回字符串而不是bool

class Form { 
var $userEmail; 
var $userPassword; 
var $hashedPassword; 
var $requiredPassLength = 6; 

public function __construct($userEmail, $userPassword) { 
    $this->userEmail = $this->cleanInput($userEmail); 
    $this->userPassword = $this->cleanInput($userPassword); 
} 

public function cleanInput($dataField) { 
    $dataField = trim($dataField); 
    $dataField = stripslashes($dataField); 
    $dataField = htmlspecialchars($dataField); 
    return $dataField; 
} 

public function isEmailEmpty() { 
    return ($this->userEmail == "" || $this->userEmail == null) ? true : false; 
} 

public function isPasswordEmpty() { 
    return ($this->userPassword == "" || $this->userPassword == null) ? true : false; 
} 

public function isEmailValid() { 
    return filter_var($this->userEmail, FILTER_VALIDATE_EMAIL); 
} 

public function isPasswordValid() { 
    return (strlen($this->userPassword) >= $this->requiredPassLength) ? true : false; 
} 

public function hashPassword() { 
    return $this->hashedPassword = password_hash($this->userPassword, PASSWORD_BCRYPT); 
} 
// More functions down here... 

現在,可以說我已經實例化一個對象通過以下方式:

$userEmail = "[email protected]"; 
$userPassword = "rootatoot"; 
$form = new Form($userEmail, $userPassword); 

因爲我isEmailValid()函數返回一個布爾值,我期望得到一個布爾返回值,即在執行時爲true/false或1/0。但是,情況並非如此。相反,當表達式計算結果爲true時,我會得到測試的電子郵件值的字符串輸出。例如,給定上面實例化的對象,語句echo $form->isEmailValid()輸出[email protected]。運行gettype($form->isEmailValid)返回string,這也不是預期的。更令人好奇的是:isPasswordValid()函數不會以這種方式表現。如果密碼實際上有效,則運行$form->isPasswordValid()返回1。如預期的那樣,運行gettype($form->isPasswordValid())返回boolean。我的代碼仍然按預期工作,這也很有趣。例如,塊

if ($form->isEmailValid()) { 
     echo "Valid email"; 
} else { 
     echo "Invalid email"; 
} 

總是給出正確的輸出。即使我的代碼按預期運行,瞭解幕後發生的情況和原因仍然很重要。所以,我對於這種行爲的問題是:

  1. 什麼是負責布爾函數返回一個字符串?例如,這是設計還是其他一些流程的副產品?
  2. 有沒有這種意外的返回值可能導致問題的任何情況?或者系統是否理解這個評估,並且始終如此處理?我的代碼現在工作正常,但我不認爲它牽強預測意外的返回值遲早會引起一些問題。

我很欣賞您提供的任何信息。謝謝!

+1

如果過濾器失敗,'filter_var'返回布爾值false;否則它返回過濾的數據 - http://php.net/manual/en/function.filter-var.php – andrewsi 2014-11-08 19:40:54

回答

1

由於我isEmailValid()函數返回一個布爾值

好,沒有,它沒有

the documentation of filter_var

返回過濾數據,或FALSE如果過濾器將失敗。

if邏輯仍然工作,因爲一個非空的字符串是「truthy」,意思是it will be treated the same as truewhen used as a condition

爲了有isEmailValid實際上返回一個布爾值,你可以在你的代碼改成這樣:

public function isEmailValid() 
{ 
    return filter_var($this->userEmail, FILTER_VALIDATE_EMAIL) !== FALSE; 
} 
+0

我明白了。如果我使用無效的電子郵件輸入執行該功能,則不會輸出任何內容。甚至不是'假'。沒有輸出自動評估爲false? – wheresmyspaceship 2014-11-08 19:49:57

+1

@ wheresmyspaceship當'false'被'echo'轉換爲一個字符串時,它變成一個空字符串。如果你想查看單詞「真」或「假」,使用'var_dump()'。 – Barmar 2014-11-08 19:56:35

+0

非常感謝你@Barmar! – wheresmyspaceship 2014-11-08 19:57:54

0

filter_var不返回一個布爾值,則返回值,如果它是有效的,否則返回false。所以,你可以重寫你的函數爲:

public function isEmailValid() { 
    return !!filter_var($this->userEmail, FILTER_VALIDATE_EMAIL); 
} 

使用!兩次將值轉換成一個布爾值。

或者,像你的其他功能,你可以寫:

public function isEmailValid() { 
    return filter_var($this->userEmail, FILTER_VALIDATE_EMAIL) ? true : false; 
} 

你並不需要在其他功能條件運算符,因爲它們使用的運算符返回布爾值,例如你可以寫:

public function isEmailEmpty() { 
    return ($this->userEmail == "" || $this->userEmail == null); 
} 

要回答你的第二個問題,它不應該是一個問題,如果你總是使用布爾值的上下文的功能,如if()。沒有有效的電子郵件應該是虛假的。

+0

我已經說過了。 – 2014-11-08 19:48:20

+0

我們都在同一時間發佈。 – Barmar 2014-11-08 19:48:54

+0

謝謝@Barmar!我很樂意接受你的回答,但@Lightness軌道比賽在那裏得到了更快。 – wheresmyspaceship 2014-11-08 19:53:01