gsingh85 Posted July 14, 2014 Share Posted July 14, 2014 I've been at this now for a while but I'm struggling to understand this code. I understand the bigger picture of what the code is doing but I can't seem to graps the logic behind the code and the steps it's taking. Here is part of the code: class Validate{ private $_passed = false, $_errors = array(), $_db = null; if($rule === 'required' && empty($value)){ $this->addError("{$item} is required"); } else if(!empty($value)){ switch($rule){ case 'min': if(strlen($value) < $rule_value){ $this->addError("{$item} must be a minimum of {$rule_value}"); } private function addError($error){ $this->_errors[] = $error; } public function errors(){ return $this->_errors; } I'm struggling to understand how the error methods and properties are being used. I can't see what is being used within the class or outside of it. Here is the code outside the class: $validate = new Validate();{ $validate = new Validate(); $validation = $validate->check($_POST, array( 'username' => array( 'required' => true, 'min' => 2, 'max' => 20, 'unique' => 'users' ), 'password' => array( 'required' => true, 'min' => 6 ), 'password_again' => array( 'required'=> true, 'matches'=> 'password' ), 'name' => array( 'required' => true, 'min' => 2, 'max' => 50 ) )); if($validation->passed()){ echo 'passed'; } else{ foreach ($validation->errors() as $error){ echo $error, '<br>'; } Looking at the 2nd part I cannot see why the programmer has put errors() as $error. Why not just create a method so you don't need to say "as $error". Also looking at the first part: private function addError($error){ $this->_errors[] = $error; } public function errors(){ return $this->_errors; } I don't understand why he has created two methods. Why not just create one? How is the public errors method able to display the errors within the class because _errors doesn't look like it contains anything. Can someone please break this down and help me to understand this because I've been at this for a while and I'm not really getting anywhere. Your help would be much appreciated. Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 14, 2014 Share Posted July 14, 2014 The class has a private variable $_errors which is an array where error messages are added when validation fails. Private variables are only visible inside an object of the class. They can not be seen or accessed outside of the class itself. So for that reason the class has a class function errors() that returns the contents of this private variable. This is why the code you have should theoretically work: if ($validation->passed()){ echo 'passed'; } else { foreach ($validation->errors() as $error){ echo $error, '<br>'; } } Your code was missing the end block but I assume that was just a copy/paste mistake. The problem with the code you provided is that I don't see where the $_passed variable gets set to true, but I will assume that it actually works somehow, perhaps by checking the contents of the $_errors array? If that's the case, there is no reason for the $_passed class variable to exist from what I can see. Doesn't seem like the best validation class? Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 14, 2014 Share Posted July 14, 2014 Just for comparison here are a couple of good/heavily used validation libraries to compare and contrast yours with: https://github.com/symfony/Validator http://framework.zend.com/manual/2.2/en/modules/zend.validator.html Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.