加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 当有__construct()元素时,将类转换为函数

发布时间:2020-12-13 17:42:55 所属栏目:PHP教程 来源:网络整理
导读:我正在学习 PHP(不断),我前段时间创建了一个处理翻译的课程.我想 emulate gettext但是从数据库中获取翻译的字符串.但是,现在我再次看到它,我不喜欢它,作为一个类,称之为我需要使用$Translate- text(‘String_keyword’);.我不想要使用$T- a(‘String_keyword
我正在学习 PHP(不断),我前段时间创建了一个处理翻译的课程.我想 emulate gettext但是从数据库中获取翻译的字符串.但是,现在我再次看到它,我不喜欢它,作为一个类,称之为我需要使用$Translate-> text(‘String_keyword’);.我不想要使用$T-> a(‘String_keyword’);因为那完全不直观.

我一直在思考如何使用简单的_(‘String_keyword’),gettext样式来调用它,但是从我从SO中学到的东西,我还没有找到一个’伟大的’实现这一目标的方法.我需要以某种方式将默认语言传递给函数,我不想在每次调用它时传递它,因为它将是_(‘String_keyword’,$User-> get(‘Language’))).我也不想在_()函数中包含用户检测脚本,因为它只需要运行一次而不是每次都运行.

最简单的就是使用GLOBALS,但我在这里已经知道它们是完全被禁止的(这可能是我can use them?的唯一情况),然后我认为DEFINE是一个变量,用户的语言就像define(USER_LANGUAGE,$User-> get(‘Language’)),但它似乎与全局相同.这是我可以看到的两个主要选项,我知道还有其他一些方法,比如依赖注入,但它们似乎为这么简单的请求添加了太多的复杂性,而我还没有时间深入研究它们.

我正在考虑首先创建一个包装器来测试它.像这样的东西:

function _($Id,$Arg = null)
  {
  $Translate = new Translate (USER_LANGUAGE);
  return $Translate -> text($Id,$Arg)
  }

这是翻译代码.在创建之前检测语言并将其传递给对象.

NOTE: This is old code 07003. They are no longer maintained and the
07004 has begun on it. See the
07005? Learn about 07006 instead,and use
07007 or 07008 – 07009 will help you decide which. If you choose
PDO,070010.

// 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->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->Lang)." FROM ".$table." WHERE keyword='".mysql_real_escape_string($Id)."'");
    $row = mysql_fetch_assoc ($results);
    return mysql_num_rows ($results) ? stripslashes ($row[$this->Lang]) : null;
    }

  // If needed to insert a name,for example,pass it in the $Arg
  public function text($Id,$Arg = null)
    {
    $Text = $this->retrieve($Id);
    if (empty($Text))
      {
      $Text = str_replace("_"," ",$Id);  // If not found,replace all "_" with " " from the input string.
      $this->add($Id,$Text);
      }
    return str_replace("%s",$Arg,$Text);    // Not likely to have more than 2 variables into a single string.
    }
  }

您将如何以适当而简单(编码)的方式实现这一目标?是否有任何建议的方法有效,或者你能提供更好的方法吗?

解决方法

如果问题只是那个问题

$Translate->text('String_keyword');

感觉很长,然后考虑通过实施__invoke将Translate对象变成Functor:

class Translate
{
    // all your PHP code you already have

    public function __invoke($keyword,$Arg = null)
    {
        return $this->text($keyword,$Arg)
    }
}

然后,您可以使用所有必需的依赖项和设置定期实例化对象并调用它:

$_ = new Translate(/* whatever it needs */);
echo $_('Hallo Welt');

这不会引入相同数量的耦合和摆弄全局范围,正如您目前考虑通过包装函数或其他地方建议的Registry / Singleton解决方案引入的那样.唯一的缺点是对象变量的非语句命名为$_().

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读