Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/217385499487252481
edited tags; edited title
Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157

Creating A Username Class For Verificationverification class

Source Link
user962206
  • 373
  • 2
  • 5
  • 13

Creating A Username Class For Verification

Everything in this code is completely working, but I still feel that this code needs to be refactored. any suggestions?

<?php
    class Db_CheckUsername{
        protected $_conn;
        protected $_username;
        protected $_minimumChars = 8;
        protected $_errors = array();
        
        public function __construct($username, $conn){
            $this->_username = $username;
            $this->_conn= $conn;
        }
        
        public function check(){
            
            $sql = "SELECT * FROM accounts WHERE username = '{$this->_username}'";
            
            $result = $this->_conn->query($sql);
            $numRows = $result->num_rows;
            
            if(preg_match('/\s/',$this->_username)){
                $this->_errors[] = "Spaces are not allowed";
            }
            
            if($numRows > 0){
                $this->_errors[] = "Username Taken";
            }
            
            if(strlen($this->_username) < $this->_minimumChars){
                $this->_errors[] = "Username Should Contain, atleast {$this->_minimumChars} chars";
            }
            
            return $this->_errors ? false : true;
        }
        
        public static function isUsernameCorrect($username,$conn){
            
        }
        
        public function getErrors(){
            return $this->_errors;
        }
    }