验证类[$ this-> firstname,...]不起作用

时间:2016-10-07 12:59:39

标签: php forms

所以我在使用$this->firstname$this->lastname等问题时遇到了问题

class Validation
    {
        private $minLength = 2;
        private $maxLength = 25;

        public function firstnameLength($firstname)
        {
            $firstnameLength = strlen($this->firstname);

            if ($firstnameLength < $this->minLength){
                return "<div class=\"alert alert-warning\"><strong>Warning!</strong> Your firstname is to short!</div>";
            }
            elseif ($firstnameLength > $this->maxLength) {
                return "<div class=\"alert alert-warning\"><strong>Warning!</strong> Your firstname is to long!</div>";
            }
        }

        public function firstnameNoSpace($firstname)
        {
            return preg_replace("[ ]", "", $this->firstname);
        }

        public function lastnameLength($lastname)
        {
            $lastnameLength = strlen($this->lastname);

            if ($lastnameLength < $this->minLength){
                return "<div class=\"alert alert-warning\"><strong>Warning!</strong> Your lastname is to short!</div>";
            }
            elseif ($lastnameLength > $this->maxLength) {
                return "<div class=\"alert alert-warning\"><strong>Warning!</strong> Your lastname is to long!</div>";
            }
        }

        public function lastnameNoSpace($lastname)
        {
            return preg_replace("[ ]", "", $this->lastname);
        }

        public function zipcodeOnlyNumbers($zipcode)
        {
            if (!is_numeric($this->zipcode)) {
                return "<div class=\"alert alert-warning\"><strong>Warning!</strong> Only numeric zipcodes allowed!</div>";
            }
        }

        public function cityNoNumbers($city)
        {
            if (preg_replace('/[0-9]+/', '', $this->city)){
                return "<div class=\"alert alert-warning\"><strong>Warning!</strong> No numbers allowed in cities!</div>";
            }
        }

        public function streetNoNumbers($street)
        {
            if (preg_replace('/[0-9]+/', '', $this->street)){
                return "<div class=\"alert alert-warning\"><strong>Warning!</strong> No numbers allowed in streets!</div>";
            }
        }

        public function usernameNoSpace($username)
        {
           if (preg_replace("[ ]", "", $this->username)){
               return "<div class=\"alert alert-warning\"><strong>Warning!</strong> No spaces allowed in username!</div>";
           }
        }

        public function usernameUnique($username)
        {
            $sql = "SELECT username FROM log_reg WHERE username = '" . mysqli_real_escape_string($this->username) ."'";
            if ($sql) {
                return "<div class=\"alert alert-danger\"><strong>Danger!</strong> Username is already taken!</div>";
            }
        }

        public function emailCheck($email)
        {
            if (!filter_var($this->email, FILTER_VALIDATE_EMAIL)){
                return "<div class=\"alert alert-danger\"><strong>Danger!</strong> Email is not valid!</div>";
            }
        }

        public function passwordNoSpace($password)
        {
            if (preg_replace("[ ]", "", $this->password)) {
                return "<div class=\"alert alert-danger\"><strong>Danger!</strong> No spaces allowed in password!</div>";
            }
        }

        public function passwordCheck($password)
        {
            if (!preg_match( '/[^A-Za-z0-9]+/', $this->password)){
                return "<div class=\"alert alert-danger\"><strong>Danger!</strong> Your password must have at least 1 uppercase letter, 1 lowercase letter and 1 number!</div>";
            }

            if( strlen( $this->password) < 8)
            {
                return "<div class=\"alert alert-danger\"><strong>Danger!</strong> Your password must have at least 8 characters!</div>";
            }
        }
    }

我想我必须在要调用的函数中定义一个像$firstname = $this->firstname这样的变量,

public function firstnameLength($firstname)
    {
        $firstname = $this->firstname;
        $firstnameLength = strlen($this->firstname);

但我得到2个错误:

1。 unused local variable
2. variable is not defined

1 个答案:

答案 0 :(得分:1)

尝试使用静态方法。您不必为简单的字符串长度检查创建验证实例。

class Validation {
    const minLength = 2;
    const maxLength = 25;

    public static function firstnameLength ($firstname) {
        if (strlen($firstname) < self::minLength){
            return "<div class=\"alert alert-warning\"><strong>Warning!</strong> Your firstname is to short!</div>";
        else if ($firstnameLength > self::maxLength)
            return "<div class=\"alert alert-warning\"><strong>Warning!</strong> Your firstname is to long!</div>";
    }
}

您可以通过静态调用获得输出

$output = Validation::firstnameLength ("SomeFirstName");