php - Returning true/false from method -


i have class before methods (not all), check if id valid. purpose of each method in class check various user details , each method returns true or false other classes can check things.

public function dosomething($userid){      $valid = false;      if(empty($userid)){          return $valid;     }      //do other stuff may turn valid true      return $valid; } 

so tidy class have moved check methods appears in, it's own method.

public function dosomething($userid){      $valid = false;      $this->idcheck($userid);      //do other stuff      return $valid; }  private function idcheck($userid){      if(empty($userid)){          return false;     }     return true; } 

if id check fails, how method - dosomething return? further check inside method:

if(!$this->idcheck($userid))return false; 

or there better way?

you did right way. return function if id invalid. otherwise - , thats better, check id in constructor!

public function __construct($userid) {     if(!empty($userid))     {         //do constructing things     } } 

as darkbee mentioned, rewrite idcheck function following:

private function idcheck($userid) {     return empty($userid); } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -