Organizing your strings in PHP

Or in any language for that matter

In a programming environment like PHP, there are chances you are dealing with strings all over your code. Whether it is to throw an error, or simply notify your users of something that happened, you have to send text messages as strings.

Here is a small example.

if (!$article){
    Error::throw404("This article cannot be found");
}

Code like this not only can be found on spaghetti code, but even in well organized frameworks. The problem with writing a string message like that is that the same message will have to be rewritten in multiple places. Let's say a little down the line, I want to throw another error if the article is not published. Here is how I would do it.

if (!$article){
    Error::throw404("This article cannot be found");
}

...

if (!$article->isPublish()) {
    Error::throw404("This article cannot be found");
}

Now I have the same string written twice. In this current blog, the same string can be found in more than 10 different places. The problem occurs if I want to make changes to the message. Let's say I don't want to use the word article anymore, I want to switch it to blog post. The only way to do so is by searching and replacing the string all over the code.

If I make slight modifications to the string, like introduce spaces by mistake, misspell a word, then I won't be able to make the changes properly.

The solution

Use a String class. This class can be organized to use a proper name space to be self explanatory when it comes to strings. We can organize error messages, status messages, or anything we need for our application. Here is an example.

class Text {
    // HTTP
    const MSG_HTTP_NOT_FOUND = "This article cannot be found";
    const MSG_HTTP_SERVER_ERROR = "There has been a server error, please try again later";

    // Database
    const MSG_DB_SQL_ERROR = "There is an error in your sql query. Please check the query.";
    const MSG_DB_CONNECTION_ERROR = "The database failed to connect. Please check your user credentials";
    ...
}

Now we can make use of this class in the code we wrote earlier.

if (!$article){
    Error::throw404(Text::MSG_HTTP_NOT_FOUND);
}

...

if (!$article->isPublish()) {
    Error::throw404(Text::MSG_HTTP_NOT_FOUND);
}

Just like that, you don't have to worry about spelling mistakes anymore. If you want to change the text, all you have to do is change it in one place and it will reflect everywhere.

This is a method I have used, but you can always make it more robust. For example, I have used arrays to store text in different languages. Example:

$string = array();
$string["en"] = array();
$string["en"]["thankyou"] = "Thank for using our services. We will send you an email shortly.";
...
$string["fr"]["thankyou"] = "Merci d'avoir utiliser nos services. Nos vous enverrons un email dans un instant.";
...

Using a similar class in conjunction of the array, you can make it much easier to interact with strings. Making modifications simply requires to update one file and it will reflect site wide.

The goal is to create an environment with minimum friction when developing. Hopefully, if you ever decide to create your own framework you will find this useful.


Comments

There are no comments added yet.

Let's hear your thoughts

For my eyes only