2011-05-02 118 views
1

我正試圖將3-4個功能合併到一個功能中,並在另一功能中調用此功能。 例如將多種功能合併爲一種功能

public function user($user) 
{ if($user==''){ $this->logerror('not valid'); //this stores error in the db 
return false; } 
} 

我多了一個電子郵件這樣

public function email($email) 
    { if($email==''){ $this->logerror('not valid'); //this stores error in the db 
    return false; } 
    } 

所以現在我想編寫一個名爲「驗證」功能,然後把上面的兩種功能。那麼當我想驗證用戶和電子郵件時,我會在其他地方稱此爲「驗證」。當我想使用它時,我不理解「驗證」函數的參數如何。如果我不清楚,請告訴我。

+0

不知道你想要什麼,但如果你想有一個想驗證這兩個,兩個調用的函數應該做的伎倆。 – 2011-05-02 18:41:55

+0

是的,它不清楚,你的意思是:'function one4all($ what,$ toDo){...}'如果是這種情況,只需使用'switch($ toDo)' – 2011-05-02 18:44:13

+0

您可能想要重命名你的功能,並改變你在他們回來的東西。我不認爲大多數人會希望名爲'user'的函數返回false或null。 – thetaiko 2011-05-02 18:46:57

回答

4

這是你想要的嗎?

public function validate($user, $email) 
{ if($user=='' and $email != ''){ $this->logerror('not valid'); //this stores error in the db 
return false; } 
} 
+0

我認爲這樣會有幫助。將在這個方向工作。感謝大家。 – geej 2011-05-03 01:41:04

1

這是你想要做的嗎?

<?php 
public function checkData($data) 
{ 
    if($data == '') { 
    $this->logerror('not valid'); //this stores error in the db 
    return false; 
    } 
} 

checkData($email); 
checkData($user); 
?> 
0
public function user($user){ 
    if($user==''){ 
     $this->logerror('name not valid'); //this stores error in the db 
     return false; 
    }else{ 
     return true; 
} 

public function email($email){ 
    if($email==''){ 
     $this->logerror('email not valid'); //this stores error in the db 
     return false; 
    }else{ 
     return true; 
    } 
} 

public function validate($name, $email){ 
    if(name($name) && email($email){ 
     // Then the form is valid 
    }else{ 
     // the form is not valid 
    } 
} 

功能驗證將驗證這兩個名稱,並通過email參數