corillo181 Posted August 22, 2007 Share Posted August 22, 2007 hey i'm building a class for a upload form and i want to know whats the best way to handle error in a class, like if someone enters a bad email or a bad password whats the best way to stop the class from keep going <?php class singUp{ private $email; private $password; private $password2; function __construct($email,$password,$password2){ $this->email = $email; $this->password = $password; $this->password2 = $password2; } function checkEmail(){ $check="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})"; if(!eregi($check,$this->email)){ return "Please check your email"; } function checkPassword(){ if(($this->password || $this->password2)==''){ return "passwords empty"; }elseif($this->password != $this->password2){ return "passwords do not match"; }elseif(strlen($this->password)<6){ return "Sorry your password is too short"; } } } } if(isset($_POST['Submit'])){ $singup =new singUp($_POST['email'],$_POST['password'],$_POST['password2']); echo $singup->checkEmail(); echo $singup->checkPassword(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/ Share on other sites More sharing options...
Ken2k7 Posted August 22, 2007 Share Posted August 22, 2007 One thing you can do is call the function and just make it return a value - true or false. Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-330713 Share on other sites More sharing options...
corillo181 Posted August 22, 2007 Author Share Posted August 22, 2007 yeah but i want it to deliver a message of what the was wrong with what the user typed, because if i just return it false with no warning they going to think there is a problem with the website. Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-330976 Share on other sites More sharing options...
lemmin Posted August 22, 2007 Share Posted August 22, 2007 One way to do it would be to return true or false, as Ken2k7 suggested, but to go along with that, have public variable $lasterror that you can put the error into. Then do something like: if (!class->function()) echo class->lasterror; Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-330992 Share on other sites More sharing options...
corillo181 Posted August 22, 2007 Author Share Posted August 22, 2007 well i took this aproach but if any one has a better way of doing this please let me know. public function sendMessage(){ if($this->checkPassword()===true && $this->checkEmail()===true){ return "everything looking good"; } } Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-331016 Share on other sites More sharing options...
corillo181 Posted August 22, 2007 Author Share Posted August 22, 2007 oh yeah i like that lemmin i can just make a error function and test to see if all the other functions are === to true.. thank you.. anything else you think i should know before i keep going ? Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-331017 Share on other sites More sharing options...
MadTechie Posted August 22, 2007 Share Posted August 22, 2007 how about this ? sorry for formatting (edited direct) <?php class singUp{ private $email; private $password; private $password2; var $errors = array(); function __construct($email,$password,$password2){ $this->email = $email; $this->password = $password; $this->password2 = $password2; } function checkEmail(){ $check="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})"; if(!eregi($check,$this->email)){ $errors[] = "Please check your email"; return false; } } function checkPassword(){ if(($this->password || $this->password2)=='') { $errors[] = "passwords empty"; return false; }elseif($this->password != $this->password2){ $errors[] = "passwords do not match"; return false; }elseif(strlen($this->password)<6){ $errors[] = "Sorry your password is too short"; return false; } } } } if(isset($_POST['Submit'])){ $singup =new singUp($_POST['email'],$_POST['password'],$_POST['password2']); $X = $singup->checkEmail(); //X= isemail valid $X =$singup->checkPassword(); //X= password valid //Show Errors print_r($singup->errors); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-331025 Share on other sites More sharing options...
corillo181 Posted August 22, 2007 Author Share Posted August 22, 2007 MadTechie you read my mind man.. lol i was going to ask for a effective way to output errors. but i had a foreach loop i guess i like your way better. Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-331045 Share on other sites More sharing options...
corillo181 Posted August 22, 2007 Author Share Posted August 22, 2007 but the array comes up empty. but i made some modification thanx to your code less code same work. <?php class singUp{ private $email; private $password; private $password2; private $error; public function __construct($email,$password,$password2){ $this->email = $email; $this->password = addslashes(htmlspecialchars($password)); $this->password2 = addslashes(htmlspecialchars($password2)); } public function checkEmail(){ $check="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})"; if(!eregi($check,$this->email)){ $this->error.="Please check your email<br />"; return false; } } public function checkPassword(){ if(($this->password || $this->password2)==''){ $this->error.="passwords empty<br />"; return false; }elseif($this->password != $this->password2){ $this->error.="passwords do not match<br />"; return false; }elseif(strlen($this->password)<6){ $this->error.="Sorry your password is too short<br />"; return false; } } public function errorMessage(){ return $this->error; } } if(isset($_POST['Submit'])){ $singup =new singUp($_POST['email'],$_POST['password'],$_POST['password2']); echo $singup->checkEmail(); echo $singup->checkPassword(); echo $singup->errorMessage(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-331054 Share on other sites More sharing options...
lemmin Posted August 22, 2007 Share Posted August 22, 2007 change the $errors[] to $this->errors[]. Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-331058 Share on other sites More sharing options...
MadTechie Posted August 22, 2007 Share Posted August 22, 2007 lol my bad, atleast you get the idea.. i find using an array is a little more useful.. as you have more control with the text at a later stage Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-331079 Share on other sites More sharing options...
corillo181 Posted August 23, 2007 Author Share Posted August 23, 2007 any one knows why this code is not working i get Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\include\db.php on line 44 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\wamp\www\include\db.php on line 44 but if i put the code simple with out a class it does work. <?php class DB{ private $user; private $pass; private $host; private $database; public $error= array(); public function __construct($username,$password,$host){ $this->user = $username; $this->pass = $password; $this->host = $host; $this->database = $database; } public function DBconnect(){ $connection = mysql_connect($this->host,$this->user,$this->pass)or die(mysql_error()); if(!$connection){ $this->error[]=$connection; } } public function DBselect($database){ $this->database=$database; if (!mysql_select_db($this->database)) { echo 'Selection of database: '.$this->database.' failed.'; return false; } } public function errorMess(){ foreach($this->error as $E){ echo $E.'<br />'; } } } $db=new DB('username','password','localhost'); $db->DBselect('test_com'); $query = "SELECT username FROM tra_users"; mysql_query($query); ?> Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-331453 Share on other sites More sharing options...
corillo181 Posted August 23, 2007 Author Share Posted August 23, 2007 oh i got it sorry. i'm just understanding classes i now know if i dont call the method it work work.. $bd->DBconnect(); Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-331463 Share on other sites More sharing options...
corillo181 Posted August 23, 2007 Author Share Posted August 23, 2007 my last question with this class is how do i make the query execute if there is no error there current method i'm using execute the curry no matter if there are errors. <?php include_once($_SERVER['DOCUMENT_ROOT'].'/include/header.php'); class singUp{ static private $email; static private $password; static private $password2; static public $error = array(); public function __construct($email,$password,$password2){ $this->email = $email; $this->password = $password; $this->password2 = $password2; } // EMAIL CHECK public function checkEmail(){ $check="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})"; $checkemail=mysql_query("SELECT email FROM tra_users WHERE email='$this->email'")or die(mysql_error()); $countemail=mysql_num_rows($checkemail); if(empty($this->email)){ $this->error['email']="You did not enter a email"; return false; }elseif($countemail>0){ $this->error['email']="Sorry that email is already in use*"; return false; }elseif(!eregi($check,$this->email)){ $this->error['email']="Please check your email"; return false; }else{ return $this->email; } } //PASSWORD CHECK public function checkPassword(){ if(($this->password=='' || $this->password2)==''){ $this->error['password']="passwords empty"; return false; }elseif($this->password != $this->password2){ $this->error['password']="passwords do not match"; return false; }elseif(strlen($this->password)<6 && strlen($this->password2)<6){ $this->error['password']="Sorry your password is too short"; return false; }else{ return $this->password; } } //ERROR MASSAGE HANDLE public function errorMessage(){ if(!empty($this->error)){ foreach($this->error as $E){ echo $E.'<br />'; } } } // WORKING ON EMAIL public function email(){ $sent=mail($this->email,$subject,$mess,$header); } //GENERATE CODE FOR ACTIVATION CHECK public function getCode(){ $rand = mt_rand(0, 32); $code = md5($rand . time()); return $code; } //COUNTS HOW MANY ERROR IN THE CODE public function errorCount(){ return count($this->error); } } if(isset($_POST['Submit'])){ $singup =new singUp($_POST['email'],$_POST['password'],$_POST['password2']); $email =$singup->checkEmail(); $password =$singup->checkPassword(); $code = $singup->getCode(); // NEED HELP MAKE THIS PART OF MY CODE WORK if(!$singup->errorCount()==0){ $query = "INSERT INTO tra_users(password,email,code,signed_up)VALUES('$password','$email','$code',now())"; echo $db->query($query); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-332246 Share on other sites More sharing options...
lemmin Posted August 23, 2007 Share Posted August 23, 2007 Put the lines: $checkemail=mysql_query("SELECT email FROM tra_users WHERE email='$this->email'")or die(mysql_error()); $countemail=mysql_num_rows($checkemail); somewhere under yout error checks. Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-332264 Share on other sites More sharing options...
corillo181 Posted August 24, 2007 Author Share Posted August 24, 2007 sorry i was talking bout the insert query but i already fixed it. my next question is.. if i got 2 classes class test1{ } class test2{ } and i define variables and methods how can i use 1 classes method side the other. Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-333157 Share on other sites More sharing options...
lemmin Posted August 24, 2007 Share Posted August 24, 2007 You could do something like: class test2{ $newclass = new test1(); $newclass->function(); } It can only go one way like that. Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-333174 Share on other sites More sharing options...
corillo181 Posted August 24, 2007 Author Share Posted August 24, 2007 thanx lemmin Quote Link to comment https://forums.phpfreaks.com/topic/66117-building-a-class/#findComment-333184 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.