Jump to content

PHP Programming/OOP5/Input validation

From Wikibooks, open books for an open world

This is an example using the power of OOP with PHP5. This example can be used to validate different user inputs. This was moved by Wykis from Kgrsajid's example on Programming:PHP.

interfaceValidator{publicfunctionvalidate($value);publicfunctiongetError();}abstractclassAbstractValidatorimplementValidator{protected$errors=array();publicfunction__construct(){// Do Something}publicfunctiongetError(){return$this->errors;}}classBooleanValidatorextendsAbstractValidator{publicfunction__construct(){// Do Something}publicvalidate($value){$return=literalize($value);if(!is_bool($value)){$this->errors[]='invalid_boolean';returnfalse;}returntrue;}}


close