Jump to content

using call_user_func_array inside class method


Hall of Famer

Recommended Posts

Well in the class UserValidator I have a public method validate(), which checks the validation type and then calls specific private methods to execute the validation script. It gets a bit tricky here since the method validate() has to decide what private method to call. I am thinking about using call_user_func_array(), the script currently looks like this below:

 

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("register", "login", "password", "session", "email", "reset", "profile", "contacts", "friends");
      foreach($this->type as $val){
         $method = "{$val}validate";
         if(in_array($val, $validarray)) call_user_func_array(array($this, $method), array());
      }      
  } 

 

The method to execute depends on the validation type passed into the validator class, so it is somewhat like a dynamic function call. The problem is, well, I am not sure if I am using call_user_func_array() properly. I read it from php manual that it needs to accept a class instance, but there is no such instance inside a class method so I use $this in the argument. Is this the correct way of using call_user_func_array()? If not, how am I supposed to do this? Thx.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.