title: Convert a class to a function when there's __construct() elements
body: <p>I am learning PHP (constantly) and I created some time ago a class that handles translations. I want to <a href="http://www.codinghorror.com/blog/2009/02/dont-reinvent-the-wheel-unless-you-plan-on-learning-more-about-wheels.html" rel="nofollow noreferrer">emulate gettext</a> but getting the translated strings from a database. However, now that I see it again, I don't like that, being a class, to call it I need to use <code>$Translate-&gt;text('String_keyword');</code>. I wouldn't like either to have to use <code>$T-&gt;a('String_keyword');</code> since that's completely not intuitive. </p>
  
  <p>I've been thinking a lot on how to make it to be called with a simple <code>_('String_keyword')</code>, gettext style but, from what I've learned from SO, I haven't been able to find a 'great' way to accomplish this. I need to pass somehow the default language to the function, I don't want to pass it every time I call it as it would be <code>_('String_keyword', $User-&gt;get('Language'))</code>). I also don't want to include the user-detection script in the <code>_()</code> function, as it only needs to be run once and not every time.</p>
  
  <p>The easiest one would be to use GLOBALS, but I've learned here that they are completely-utterly forbidden (could this be the only case where I <a href="http://c2.com/cgi/wiki?GlobalVariablesAreBad" rel="nofollow noreferrer">can use them?</a>), then I thought to <a href="http://php.net/manual/en/function.define.php" rel="nofollow noreferrer">DEFINE</a> a variable with the user's language like <code>define ( USER_LANGUAGE , $User-&gt;get('Language') )</code>, but it seems just to be the same as a global. These are the 2 main options I can see, I know there are some other ways like Dependency Injection but they seem to add just too much complication for a so simple request and I haven't yet had time to dig into them.</p>
  
  <p>I was thinking about creating a wrapper first to test it out. Something like this:</p>
  
  <pre><code>function _($Id, $Arg = null)
    {
    $Translate = new Translate (USER_LANGUAGE);
    return $Translate -&gt; text($Id, $Arg)
    }
  </code></pre>
  
  <p>Here is the translation code. The language is detected before and passed to the object when created.</p>
  
  <pre><code>// Translate text strings
  // TO DO: SHOULD, SHOULD change it to PDO! Also, merge the 2 tables into 1
  class Translate
    {
    private $Lang;
  
    function __construct ($Lang)
      {
      $this-&gt;Lang = $Lang;
      }
  
    // Clever. Adds the translation so when codding I don't get annoyed.
    private function add ($Id, $Text)
      {
      $sql="INSERT INTO htranslations (keyword, en, page, last) VALUES ('$Id', '$Text', '".$_SERVER['PHP_SELF']."', now())";
  
      mysql_query($sql);
      }
  
    private function retrieve ( $Id )
      {
      $table = is_int ($Id) ? "translations" : "htranslations";      // A small tweak to support the two tables, but they should be merged.
      $results = mysql_query ("SELECT ".mysql_real_escape_string($this-&gt;Lang)." FROM ".$table." WHERE keyword='".mysql_real_escape_string($Id)."'");
      $row = mysql_fetch_assoc ($results);
      return mysql_num_rows ($results) ? stripslashes ($row[$this-&gt;Lang]) : null;
      }
  
    // If needed to insert a name, for example, pass it in the $Arg
    public function text($Id, $Arg = null)
      {
      $Text = $this-&gt;retrieve($Id);
      if (empty($Text))
        {
        $Text = str_replace("_", " ", $Id);  // If not found, replace all "_" with " " from the input string.
        $this-&gt;add($Id, $Text);
        }
      return str_replace("%s", $Arg, $Text);    // Not likely to have more than 2 variables into a single string.
      }
    }
  </code></pre>
  
  <p><strong>How would you accomplish this in a proper yet simple (for coding) way?</strong> Are any of the proposed methods valid or can you come with a better one?</p>