pocobueno1388 Posted September 17, 2007 Share Posted September 17, 2007 I am trying to set an array full of errors from inside my class, but I would like to be able to use that array outside of my class so I can echo it out. Here is the code to the function inside the class. <?php function setEmail($email){ $emailChk = mysql_query("SELECT email FROM users WHERE email='$email'"); //Make sure it's valid if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) || $email == ""){ return $error[] = "Email is not valid"; return $error[] = "2nd error"; //Make sure the email isn't already in use } else if (mysql_num_rows($emailChk) > 0){ return $error[] = "This email is already in use by another account."; } else { //everything is good, change the email. } } ?> Here is how I am using the class. <?php $user = new user; $user->setEmail("email_goes_here"); print_r($error); ?> How could I get the $error array able to be used outside of the class while still containing all the values it picked up from inside the class? Also, if the $error array already had some values in it BEFORE I used the class, would it be possible to have the class add on to the array, then be able to be used outside the class with every error still intact with it? Hopefully I explained this well enough. I just started using OOP, so If I'm completely going about this the wrong way, please let me know and direct me to the right way I appreciate your help, thanks =] Link to comment https://forums.phpfreaks.com/topic/69693-solved-making-a-variable-defined-inside-a-class-accessable-outside/ Share on other sites More sharing options...
ToonMariner Posted September 17, 2007 Share Posted September 17, 2007 print_r($user->error); Link to comment https://forums.phpfreaks.com/topic/69693-solved-making-a-variable-defined-inside-a-class-accessable-outside/#findComment-350167 Share on other sites More sharing options...
pocobueno1388 Posted September 17, 2007 Author Share Posted September 17, 2007 Okay, I figured out how to get the errors to display out of the class. I did it like this: I added the error variable like this: <?php class user { var $userID; var $username; var $money; var $rank; var $account_type; var $email; var $password; var $error; //<------added ?> Then I assigned the errors to the variable from inside the class (obviously) like this: $this->error[] = "Error"; Then could access them like this: $user = new user($sid); $user->setEmail("email_goes_here"); print_r($user->error); NOW the problem is that I want the $error variable to be set to the $error variable that already exists from outside the class. So how I would I go about doing that? I figured it will be something like this: class user { var $error = global $error; That didn't work though...it threw a syntax error. Link to comment https://forums.phpfreaks.com/topic/69693-solved-making-a-variable-defined-inside-a-class-accessable-outside/#findComment-350174 Share on other sites More sharing options...
ToonMariner Posted September 17, 2007 Share Posted September 17, 2007 $errors = $user->setEmail("email_goes_here"); print_r($errors); Link to comment https://forums.phpfreaks.com/topic/69693-solved-making-a-variable-defined-inside-a-class-accessable-outside/#findComment-350180 Share on other sites More sharing options...
pocobueno1388 Posted September 17, 2007 Author Share Posted September 17, 2007 ToonMariner -> See my above post, I edited it. Link to comment https://forums.phpfreaks.com/topic/69693-solved-making-a-variable-defined-inside-a-class-accessable-outside/#findComment-350188 Share on other sites More sharing options...
pocobueno1388 Posted September 17, 2007 Author Share Posted September 17, 2007 I guess I can just do this: $error = array_merge($error, $user->error); Thanks for the help =] Link to comment https://forums.phpfreaks.com/topic/69693-solved-making-a-variable-defined-inside-a-class-accessable-outside/#findComment-350192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.