0

I am fairly new to using PHP with classes and wondered if there is a better method for what I'm doing. Basically I am looking for the best way to handle user errors (like "That username is taken" etc).

What I am doing..

In init.php

global $errors;
$errors  = array();
require ...

In classname.php

class Test {
 public function myFunction($username) {

  if ... {
    //Do Something
    global $errors;
    $this->errors = $errors[] = "Username already in use!";
  }

  else ... { 
    global $errors;
    $this->errors = $errors[] = "Username already in use!";
  }
 }
 public function .... {}
}

Basically is there a way I can use the global array without having to re-write global $errors every time? Having to do repeat it just doesn't feel efficient which in my case usually means there is a better way. Any ideas?

2
  • You can do global $errors; once at the top of classname.php. Commented May 8, 2014 at 23:25
  • You could declare a function (addError($str) for example) that handles the global $error array and simply call that in your class. Commented May 8, 2014 at 23:39

2 Answers 2

2

Basically any time you have to declare a variable global there's likely a better way to go about what you're doing that will have you writing clearer, more maintainable code.

Here are two methods I stick to to handle what you're up against.

class Foo {

  // example 1: exceptions
  public function newUser1($username) {
    if( $this->userExists($username) ) {
      throw new Exception("User already exists: $username");
    }
  }

  // example 2: pass-by-reference
  public function newUser2($username, &$errors) {
    if( $this->userExists($username) ) {
      $errors[] = "User already exists: $username";
      return
    }
  }

}

$inst = new Foo();
$errors = array();

// example 1: exception handling
try {
  $foo->newUser1('Sammitch');
} catch ( Exception $e ) {
  $errors[] = $e->getMessage();
}

//example 2: pass-by-reference
$foo->newUser2('Sammitch', $errors);
if( count($errors) > 1 ) {
  // oh noes!
}

The one restriction of Exceptions is that when your throw it execution stops and the exception either goes into the catch block or, if there are no catch block, the exception bubbles up until it becomes a fatal PHP error.

Sign up to request clarification or add additional context in comments.

Comments

2

I would suggest you inject your $errors instead of globalizing it. That way you don't have to track down where it's being set/called/etc

 class Test {
     public function __construct(Array $errors) {
         $this->errors = $errors;
     }
 }

 $test = new Test($errors);

2 Comments

Injecting is great, but the data does not come back out! Arrays are no objects, and will not be passed as reference.
@Sven sure they do. $test->errors or $test->getErrors(), etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.