oop - PHP multiple interfaces to implement validation functionality? -
i trying implement validation functionality on website on working.
i have created interface entitled validator classes implement. single function in interface validate($input).
then have class e.g emailvalidator implements this. in implementation of validate() checks if input valid email , returns true or false respectively.
now.. want implement check whereby 2 inputs sent , check if equal. example in case of passwords. interface function takes 1 input not suitable.
i can create second interface validate function takes 2 inputs e.g pairvalidator. optimal way implement want?
my concern arise because due possibility of validation functions may need plausibly have loads of interfaces e.g setvalidator checking if value in set..
thanks
there couple of ways can go this. couple of easy ones won't break current interface definition are:
1) present needs validated array.
$validator->validate($_post["password"],$_post["password_repeat"]);
2) construct validator 1 of values need.
$validator = new validateequals($_post["password"]); $validator->validate($_post["password_repeat"]);
one way can break current interface , require changing validators have remove parameter altogether interface definition , use position parameters via func_get_args
interface validator { public function validate(); } class equalsvalidator implements validator { public function validate(){ $args = func_get_args(); return $args[0] == $args[1]; } }
Comments
Post a Comment