PHP从多个函数访问变量

Novice question but then I am a novice at this...

I have a class for registering users and I have functions for different validation. Basically, I run "mainTest" which pulls in the variables from a form and then runs each function (test1, test2). Within those functions if something doesn't validate within the function I set a variable called $error to true. What I am trying to do within the "mainTest" function check if the variable $error has been set to true in any of the other functions do something but when I echo the $error variable it just says null i.e. the functions aren't linking. (trying not to use Global Variables).

Any Ideas here is an example of how I am doing this...

class myClass {
 private function test1($var1, $var2) {
  if....
  else {
   $error = true;
   return $error;
  }
 }

 private function test2($var3, $var4) {
  if....
  else {
   $error = true;
   return $error;
  }
 }

 public function mainTest($var1, $var2, $var3, $var4) {
  $this->test1($var1, $var2);
  $this->test2($var3, $var4);

  if ($error == true) {
   //do something
  }
 }
}

use an instance variable in the class ie..

class myClass {
    private $error = '';
    function test2()
    {
         //some code here
         $this->error = true;
         return $this->error;
    }

Now you can access the error variable in other function using the $this keyword $this->error, here's a little reading

Since you're returning the TRUE boolean, you can check to see each function's output to see if it's TRUE, or not:

public function mainTest($var1, $var2, $var3, $var4) {
  if ($this->test1($var1, $var2) === TRUE) || $this->test2($var3, $var4) === TRUE) {
    // an error occurred, because your function returned a TRUE boolean
  }
}

If you'd want to re-structure your code, you can declare a public variable as part of your class and set it using your functions.

class myClass {
    // ...
    private $error;
    private function test1($var1, $var2) {
        if....
        else {
            $this->error = true;
        }
    }
    // ...
    public function mainTest($var1, $var2, $var3, $var4) {
          $this->test1($var1, $var2);
          $this->test2($var3, $var4);
          if ($this->error === TRUE) {
               // an error occurred
          }
    }
}

From what you have submitted in the way of code, all of your functions return a boolean. This means that inside of your mainTest you can do something like this:

$error = test1() || test2();

This means that if either of test1 or test2 return true, error will be set to true. But if they both fail, error is false. You can use the || operator any number of times, take my trivial example for instance:

    function one(){
        return false;
    }

    function two(){
        return false;
    }

    function three(){
        return false;
    }

    $var = one() || two() && three();

    var_dump($var);

In this example, var will be a boolean variable set to false. However, if any one of the three functions returned true, var would of corse be true.

With this, you are semantically saying "the result of one OR the result of two OR the result of three." With || true takes precedence over false, meaning that is anything is true, the end result is true.

In your example, the problem with using the class variable approach as others have suggested, is that, what if test1() sets error to true and then test2() later sets it to false. This can of corse be overcome, however it would require more logic in your mainTest function. Essentially you would need to check the value of error after each function.