Checking PHP Objects

I am currently working on an OO PHP application. I have a class called validation that I would like to use to validate all the data presented, but I obviously need to define rules somewhere for each property being checked. I am currently using arrays when creating a new object. eg:

$this->name = array( 'maxlength' => 10, 'minlength' => 2, 'required' => true, 'value' => $namefromparameter ) 

One array for each property.

Then I called the static method from the validation class, which would perform various validations depending on the values ​​defined in each array.

Is there a more efficient way to do this? Any advice is appreciated. Thanks.

+6
object php validation
source share
3 answers

I know that an associative array is usually used to set things up in PHP (it is called a magic container and is considered bad practice, by the way), but why don't you create several validator classes, each of which can handle one rule? Something like that:

 interface IValidator { public function validate($value); } $validators[] = new StringLengthValidator(2, 10); $validators[] = new NotNollValidator(); $validators[] = new UsernameDoesNotExistValidator(); 

This has several advantages over array implementation:

  • You can document them (very important), phpdoc cannot parse comments for array keys.
  • Your code will become safe ( array('reqiured' => true) )
  • It is completely OO and does not introduce new concepts.
  • This is more readable (albeit more verbose)
  • The implementation of each constraint can be found intuitively (this is not in the 400-line function, but in the corresponding class)

EDIT . Here is a link to the answer I gave , to another question , but this basically applies to this as well.

+8
source share

Since using OO would be cleaner if you used classes to check properties. For example.

 class StringProperty { public $maxLength; public $minlength; public $required; public $value; function __construct($value,$maxLength,$minLength,$required) { $this->value = $value; $this-> maxLength = $maxLength; $this-> minLength = $minLength; $this-> required = $required; } function isValidat() { // Check if it is valid } function getValidationErrorMessage() { } } $this->name = new StringProperty($namefromparameter,10,2,true); if(!$this->name->isValid()) { $validationMessage = $this->name-getValidationErrorMessage(); } 

Using a class has the advantage of encapsulating the logic inside it that an array (basically a structure) does not have.

0
source share

Maybe inspired by the Zend-Framework Validation .

So, define the wizard:

 class BaseValidator { protected $msgs = array(); protected $params = array(); abstract function isValid($value); public function __CONSTRUCT($_params) { $this->params = $_params; } public function getMessages() { // returns errors-messages return $this->msgs; } } 

And then create your own validators:

 class EmailValidator extends BaseValidator { public function isValid($val=null) { // if no value set use the params['value'] if ($val==null) { $val = $this->params['value']; } // validate the value if (strlen($val) < $this->params['maxlength']) { $this->msgs[] = 'Length too short'; } return count($this->msgs) > 0 ? false : true; } } 

Finally, your inline array may become something like:

 $this->name = new EmailValidator( array( 'maxlength' => 10, 'minlength' => 2, 'required' => true, 'value' => $namefromparameter, ), ), ); 

verification can be performed as follows:

 if ($this->name->isValid()) { echo 'everything fine'; } else { echo 'Error: '.implode('<br/>', $this->name->getMessages()); } 
0
source share

All Articles