如何从PHP中的构造函数方法返回错误

时间:2011-11-29 03:06:02

标签: php oop object constructor

我试图通过OOP方式学习PHP Web开发,并且有一个关于如何从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;
    }
}
?>

1 个答案:

答案 0 :(得分:1)

理想情况下,您将使用单独的类来验证表单数据。见this post

您还可以添加一个valid()方法执行 User 类:

function valid(){
  return count($this->errors);
}

然后,根据需要调用该函数以验证数据。

$user = new User(...);
if($user->valid()){
    echo 'the information is valid.';
}else{
    echo 'the information is invalid.';
}