将多个功能组合到一个功能中

时间:2011-05-02 18:38:44

标签: php function

我试图在一个函数中组合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; } 
    } 

所以现在我想编写一个名为“validate”的函数,然后将上面两个函数放在上面。当我想验证用户和电子邮件时,我会在其他地方称之为“验证”。当我想使用它时,我不明白参数将如何用于“验证”功能。如果我不清楚,请告诉我。

3 个答案:

答案 0 :(得分:4)

这是你想要的吗?

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

答案 1 :(得分:1)

这是你想要做的吗?

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

checkData($email);
checkData($user);
?>

答案 2 :(得分: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
    }
}

函数validate将验证传递的名称和电子邮件参数

相关问题