I would not use global
I would use normal
way
<?php
$ERRORS = array(
'0' => "Unauthorized access",
'1' => "Wrong username / password",
'2' => "Missing field(s)",
'3' => "Passwords do not match!"
);
var_dump($ERRORS);
function getErrorMessage($errorCode, $ERRORS)
{
var_dump($ERRORS);
// More code to output the corresponding message
}
Global is trash IMO, because there is no way to tell were it was set. Sure in a single file that seems fine, but when you have a site composed of hundreds of files and 10s of thousands of lines of code, it can be near impossible to find where a global was set or changed. (I've spent hundreds of hours digging though older 3rd party code for stuff like that )
Personally I avoid using it at all cost.
It would be even better to make this a class with a getErrors() method, that's how I would do it ( except I would build an actual \Exception class )
final class AppError
{
//constant value should match array key for the error
/*
Unknown Error was intentionally left out, this is for internal
use granted with only 4 errors and the constants it would be
hard to give an invalid error code, but as the class grows
we are future proofing it.
*/
const ER_ACCESS = 0;
const ER_LOGIN = 1; //this is a login error it seems, and this looks better / is shorter then ER_USER_PASS
const ER_MISSING = 2;
const ER_PASSWORD = 3;
public static $ERRORS = [
'-1' => "Unknown Error", //I added this
//'0' => "No Error", //I changed this number
'1' => "Unauthorized access",
'2' => "Wrong username / password",
'3' => "Missing field(s)",
'4' => "Passwords do not match!" //isn't this the wrong password
];
private function __construct(){ //no construction }
public static function getErrors( $code = null)
{
//on null return all
if(in_null($code))
return self::$ERRORS;
//on false or 0 return empty string
if(!$code) return '';
/*
Example:
Consider this, you could have a function that checks for
errors and returns an error code or false on no errors. So,
by returning an empty string here we can avoid having to check
before passing that into this class.
*/
//code not set in array = unknown error
if(!isset(self::$ERRORS[$code]))
return self::$ERRORS['-1'];
return self::$ERRORS[$code];
}
}
Then
var_dump( AppError::getErrors() );
function getErrorMessage($errorCode)
{
var_dump( AppError::getErrors() );
// More code to output the corresponding message
}
And now you can do this too ( as a bonus )
var_dump(AppError::getErrors(0)); //"Wrong username"
Now you may wonder why I added these const E_* = #
?
If you fill your code up with things like the above example( numbered codes). And then later after you add a bunch of errors, you decide to organize them and renumber them you will have to look through all your code and find those number and fix them. Using a constant we don't care what value the constant is, it could be pumpernickel
and it wouldn't matter.
it's hard to look at 0
and know its a Unauthorized access
error. So, by using the constants we can improve the readability of the code overall. When you see ER_ACCESS
instead of 0
it will be clear what it is.
Summery this moves all the dependencies inside of the class were we can keep everything tidy. Basically it's a separation of concerns, your code outside the class shouldn't care what the value of that error code is, mainly you just want the correct string message. How that's done is a black box, the implementation of which doesn't matter to the rest of your application.
You may also notice I used final
for the class and a private
constructor. These protect the class from being modified later or used in a way it's not intended. Final means the class cannot be extended, and therefor none of the methods can be modified by child class. PHP will throw an error if you try to create a new instance of the class with new AppError()
. This means the only way to access the methods in the class is with the static call class::method()
.
It's kind of a picky thing, but I thought I would do it as I would wright the class in the real
for sake of completeness. I've also went a bit above and beyond with the explanation, that's because I have no idea of your level of Object Oriented Programming and using a class as I outlined is IMO the correct way to do this.
Now its very simple to use the constants( mirroring the above example )
var_dump(AppError::getErrors(AppError::ER_ACCESS)); //"Unauthorized access"
Note I didn't test any of this so if there are any syntax errors, forgive me.
The last thing I would suggest is to take a look at how Exceptions work in php, all the cool kids use them, and it will let you do things like throw
and catch
errors.
http://php.net/manual/en/language.exceptions.php
P.S. the question was already answered when I wrote this ( I don't care about the points ) just the OP seemed to be nice and had a well written question, so I thought I would put something comprehensive together to show the OOP possibilities.
Enjoy!