Well I'm having a user validator class, which is instantiated to evaluate user data/inputs and send back error messages if problems are detected. This is the way I design it so far, and I am a bit confused with the call_user_funcs_array(). The script is shown below:
<?php
class UserValidator extends AbstractValidator{
// The user validator for user and usergroup system
private $user;
private $usergroup;
public function __construct(User $user, $type = NULL, $value = NULL){
// Fetch the basic properties for usergroup
parent::construct($type, $value);
$this->user = $user;
$this->usergroup = $user->getgroupobj($user->usergroup);
// Successfully instantiate the user validator object
}
public function validate(){
// The core method validate, it sends requests to different private methods based on the type
if(empty($this->type)) throw new Exception('The validation type is empty, something must be seriously wrong...');
if(is_array($this->type) and !is_array($this->value)) throw new Exception('Cannot have scalar value if the type is an array.');
if(!is_array($this->type) and is_array($this->value)) throw new Exception('Cannot have scalar type if the value is an array.');
// Now we are validating our user data or input!
$validarray = array("uid", "username", "password", "session", "email", "ip", "usergroup", "money", "friends");
foreach($this->type as $val){
$method = "{$val}validate";
if(in_array($val, $validarray)) call_user_func_array(array($this, $method), array());
}
}
private function uidvalidate(){
// The user id Validator
}
private function usernamevalidate(){
// The user name Validator
}
private function passwordvalidate(){
// The password Validator
}
private function emailvalidate(){
// The email validator
}
private function sessionvalidate(){
// The session validator
}
private function ipvalidate(){
// The ip validator
}
private function money validate(){
// The user money validator
}
}
?>
As you can see from the above code, the method to execute is not hard-coded as in most cases. If a match between validator type and method is available, the script will execute a method as planned. I heard all_user_func_array() can be used to execute functions/methods dynamically, if the function/method execution is dynamic. However, the first argument of the all_user_func_array() needs to accept an instance of class, while I am calling it inside a class template. So my questions are:
- I pass $this to the argument, is this gonna work?
- If not, how can I call a method by assigning a method to a variable?
- Incase you do not mind, please lemme know if there is a more efficient way of designing a validator class?
Id appreciate answers to any of the three questions,