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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.