Hall of Famer Posted April 15, 2012 Share Posted April 15, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261004-using-call_user_func_array-inside-class-method/ Share on other sites More sharing options...
KevinM1 Posted April 16, 2012 Share Posted April 16, 2012 Have you tried simplifying it to just $this->$method() ? Quote Link to comment https://forums.phpfreaks.com/topic/261004-using-call_user_func_array-inside-class-method/#findComment-1337711 Share on other sites More sharing options...
Hall of Famer Posted April 16, 2012 Author Share Posted April 16, 2012 Have you tried simplifying it to just $this->$method() ? Oh really? Thats gonna work? o_o Quote Link to comment https://forums.phpfreaks.com/topic/261004-using-call_user_func_array-inside-class-method/#findComment-1337730 Share on other sites More sharing options...
KevinM1 Posted April 16, 2012 Share Posted April 16, 2012 Have you tried simplifying it to just $this->$method() ? Oh really? Thats gonna work? o_o You tell me. Quote Link to comment https://forums.phpfreaks.com/topic/261004-using-call_user_func_array-inside-class-method/#findComment-1337737 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.