Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

From reading the faq, basically I would like if everyone could give me advice on all of the things enunciated there about my code (mainly design pattern usage and security):

  • Best practices and design pattern usage
  • Security issues
  • Performance
  • Correctness in unanticipated cases

Here is it. I think it has no errors and everything is covered. Also I think the comments are pretty clear about how to use it so no need for much more explanation:

<?php
/* 
* Transladb
* @Version  0.8
* @Author   Francisco Presencia Fandos
*/
class Translate
  {
  private $Lang;
  private $DB;

  /* Pass the right parameters. */
  public function __construct ($DB, $Lang)
    {
    $this->DB = $DB;
    $this->Lang = $Lang;
    }

  /* Requires PHP 5.3+. Simplifies the method calling to $_("keyword"); */
  public function __invoke($Id, $Arg = null)
    {
    $Id = str_replace(" ", "_", $Id);
    if (!($Text = $this->retrieve($Id)))
      {
      /* Adds the string dynamically to database so it doesn't break the flow. */
      $Text = str_replace("_", " ", $Id);
      $this->add($Id, $Text);
      }
    return str_replace("%s", $Arg, $Text);    // Not likely to have more than 2 variables into a single string.
    }

  /* If the string doesn't exist it creates it. Useful not to break the coding flow. */
  private function add ($Id, $Text)
    {
    $STH = $this->DB->prepare("INSERT INTO translations (keyword, en, last) VALUES (?, ?, now())");
    $STH->execute(array($Id,$Text));
    }

  /* It returns the right cell, however it might be empty. No default language option. */
  private function retrieve ($Id)
    {
    $STH = $this->DB->prepare("SELECT * FROM translations WHERE keyword=? LIMIT 1");
    $STH->execute(array($Id));

    $row = $STH->fetch();
    return $row[$this->Lang];
    }
  }

/* Create the object. You might want to change this line somewhere else. */
$_ = new Translate;
echo $_("This is a test to show how %s works", "TranslateDB");
?>

In case anyone is interested, I'm developing it here: https://github.com/FranciscoP/transladb

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.