可以在 PHP 类中使用 GetText 吗?
例如:
class User {
private $sex = 1;
public function getSex() {
if($this->sex == 1)
return __('Male');
elseif($this->sex == 2)
return __('Female');
}
}
我认为最好不要在课堂上使用 gettext,但我不知道在这种情况下我能做什么。
For example :
class User {
private $sex = 1;
public function getSex() {
if($this->sex == 1)
return __('Male');
elseif($this->sex == 2)
return __('Female');
}
}
I believed that it's better to not use gettext inside class, but I don't know what can i do in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 PHP 中,在对象内部使用全局函数是可以接受的——因为无论如何,大多数内置库都是全局函数,如果不是这样的话,那真的没有意义。在这种情况下,由于您使用的是外部库,因此我认为这实际上是最谨慎的方法。
附带说明:作为
__('Unknown');
或 < code>__('Other');if(a == b){}elseif(a == c){}elseif(a ==d ).. .
,你最好使用switch
语句。In PHP it is acceptable to use global functions inside of objects — since most of the built in libraries are global functions anyway, it wouldn't really make sense for this not to be the case. In this case, since you are using an external library, it actually strikes me as the most prudent approach.
As a side note:
__('Unknown');
or__('Other');
if(a == b){}elseif(a == c){}elseif(a ==d )...
, you're better off using theswitch
statement.