0

I store some attributes and their options in class Attributes.

public static $ethnicity        = array(0 => '-', 
                                        1 => 'Asian',
                                        2 => 'Black / African descent',
                                        3 => 'East Indian',
                                        4 => 'Latino / Hispanic',
                                        5 => 'Middle Eastern',
                                        6 => 'Native American',
                                        7 => 'Pacific Islander',
                                        8 => 'White / Caucasian',
                                        9 => 'Other');  

Why I keep them in code and not db is another story. Now I have problem about poedit because it can not read dynamic translations. I have now two options:

a) Put all array values to one single dump file. This file will be used only by poedit parser:

_('Asian'), _('Black / African descent'), ...

In this way I can call translations in view in the following way:

echo _(Attributes::$ethnicity[3]));

b) I can use constructor and call gettext from there.

class Attributes 
{ 

    public function __construct()
    {
        $this->ethnicity        = array(0 => _('-'), 
                                        1 => _('Asian'),
                                        2 => _('Black / African descent'),
                                        3 => _('East Indian'),
                                        4 => _('Latino / Hispanic'),
                                        5 => _('Middle Eastern'),
                                        6 => _('Native American'),
                                        7 => _('Pacific Islander'),
                                        8 => _('White / Caucasian'),
                                        9 => _('Other'));  
      //...      
    }                                    
}

In this way I can call translations in view in the following way:

$attr = new Attributes;
echo $attr->ethnicity[3]; 

Now to my questions:

Let's say that there will be about 10 attributes like this and on average 40 options, so this is all together 400 array pairs. Will using construct in this way slow application at any way? Because this would mean that everytime I call constructor, gettext will be called for all array values, even if this attribute value is not displayed in view. If I don't use constructor, than gettext will be called only for single attributes values which will be actually used.

I did an ab test, but was surprised because there weren't any difference. But I am worried that I must be missing something as my sense would say that calling constructor in this way will make some slowing.

user1324762
  • 765
  • 4
  • 7
  • 24

1 Answers1

1

I personally would stick with solution B as it's cleaner and after few months you still know what $attr->ethnicity[3]; does.

If you are still unsure, try to do some stress test - try to call the constructor many times. But my humble opinion is that there will be no significant difference. Especially if you don't need to call it that many times in live application. gettext is designed to be fast.

mato
  • 173
  • 8