phpnaming-conventions

Configurations, Registry and Language names conventions


Well, here's a great question that has always disturbed me.

Introduction

I'm developing a web application that should be multi-language and customizable. So I developed a registry-class (that will group my internal configurations) and a language class that will take care of getting the right language file (based on the browser language) and get the language string I need.

Let's get deeper

Let's talk about the use of this classes

# config.reg.php
self::set('configuration-name');
# whatever-file.php
reg::get('configuration-name');

The same piece of code is used for the language class which will have a

# it.lang.php
self::set('duck-you', 'Daffanculo');
# en.lang.php
self::set('duck-you', 'Duck you');
# whatever-file.php
lang::get('duck-you');

The real matter

Ok, now. I'm always pretty much d*cked while choosing the right name for the configurations and for the language indexes, and I always end up with something like:

self::set('that-error-message-that-appear-when-a-user-is-not-logged-in');

Or:

self::set('your-login-action-has-encountered-a-problem-please-get-back-later');

for languages, and:

self::set('that-cookie-name-used-in-X-file');

for configurations. ...which is not that great.

Final question

So I was wondering, are there any names conventions that could help me getting through this and move on?


Solution

  • for starters your language values should not be encapsulated in your registry object.

    your registry object should only contain objects such as Session, Language, Input, output etc, so your static class should be build like so:

    abstract class Registry
    {
        public static function get($key)
        {
        }
    
        public static function set($key,$object)
        {
        }
    }
    

    then you would insert your objects into the Registry like so:

    Registry::set("Language", new Language());
    

    You should then use your language class to fetch your language files:

    $Language = Registry::get("Language");
    $Message = $Language->get("YourMessageKey");
    

    in regards to naming conventions I would advice you to use all lowercase, and underscores only (this is for clarity)

    so the following line:

    that-error-message-that-appear-when-a-user-is-not-logged-in
    

    would become

    that_error_message_that_appear_when_a_user_is_not_logged_in
    

    but this is way to long and pointless, what you should do is have your language multi-dimensional so that the error messages are contained within the error context.

    $Language->errors("user_not_logged_in");
    

    which becomes much more readable and has better performance because you would split your context's up into separate files like so

    /locale/en/primary.php | loaded by default
    /locale/en/errors.php
    /locale/en/menu.php
    /locale/en/user.php
    

    Each would only be loaded when called, for example:

    $Language->user("welcome_new_user");
    /*         ^                      */
    

    In your language class create a call method to be able to do this kind of selecting:

     public function __call($context,$params)
     {
         if(!$this->contextLoader($context))
         {
             $this->loadContext($context)
         }
         return $this->languages[$this->lid][$context][$params[0]];
     }
    

    This will make things much more manageable.

    Note:

    Wash your dirty mouth out!