Jump to content

[SOLVED] Making a variable defined inside a class accessable outside.


pocobueno1388

Recommended Posts

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
Share on other sites

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