对于这些嵌套的 if/elseif 语句,更优雅的解决方案是什么?
我正在构建一个包含具有用户配置文件的用户的网站。配置文件中的许多字段都是可选的。
存在大量用户生成内容的机会,因此我需要在网站的许多不同位置(评论、帖子等)显示该内容的作者。在用户的个人资料中,他能够(可选)填写他的“名字”、“姓氏”和“显示名称”。
为了显示作者,我编写了一个辅助方法,该方法查看提供的这些字段的数组,并按照以下优先顺序返回最适合用户的名称:
- 如果用户填写了
display_name
,则此将显示。 - 如果用户填写了
first_name
和last_name
,但没有填写display_name
,则会显示两个名字 - 如果用户只填写了
first_name< /code>,它将显示
first_name
。 - 如果用户只填写
last_name
,则会显示last_name
。 - 如果所有其他方法都失败,将显示用户 ID,即
user123
- 如果没有任何数组键存在,或者参数为 NULL,则名称将显示为
NULL
该方法效果很好,但很丑。必须有一种方法可以用嵌套 if/else 语句的替代方案来美化它。
public function nameify($names = NULL) {
$name = '';
if (!empty($names)) {
if (!empty($names['display_name'])) {
$name = $names['display_name'];
} elseif (!empty($names['first_name'])) {
$name = $names['first_name'];
if (!empty($names['last_name'])) {
$name .= ' ' . $names['last_name'];
}
} elseif (!empty($names['last_name'])) {
$name = $names['last_name'];
}
if (empty($name) && !empty($names['id'])) {
$name = 'user' . $names['id'];
} else {
$name = 'NULL';
}
} else {
$name = 'NULL';
}
return $name;
}
I'm building a website that contains users with user profiles. Many of the fields in the profile are optional.
There is an opportunity for a lot of user-generated content, and so I need to display the author of this content in many different locations of the site (comments, posts, etc.). In the user's profile, he is able to (optionally) fill out his "first name", his "last name", and a "display name".
To display the author, I wrote a helper method that looks through a provided array of these fields and returns the most appropriate name for the user, in this order of preference:
- If the user filled out
display_name
, this will be displayed. - If the user filled out
first_name
andlast_name
, but nodisplay_name
, it will display both names - If the user only filled out
first_name
, it will displayfirst_name
. - If the user only filled out
last_name
, it will displaylast_name
. - If all else fails, a user id will be displayed i.e.
user123
- If none of the array keys are present, or the parameter is NULL, the name will display as
NULL
The method works great, but it's ugly. There must be a way to beautify this with an alternative to nested if/else statements.
public function nameify($names = NULL) {
$name = '';
if (!empty($names)) {
if (!empty($names['display_name'])) {
$name = $names['display_name'];
} elseif (!empty($names['first_name'])) {
$name = $names['first_name'];
if (!empty($names['last_name'])) {
$name .= ' ' . $names['last_name'];
}
} elseif (!empty($names['last_name'])) {
$name = $names['last_name'];
}
if (empty($name) && !empty($names['id'])) {
$name = 'user' . $names['id'];
} else {
$name = 'NULL';
}
} else {
$name = 'NULL';
}
return $name;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
首先设置默认值,如果没有其他匹配,则返回默认值。然后,因为我们总是希望返回显示名称(如果我们这样做的话)。
编辑:调整以防止返回“NULL”
Set the default first, and return that if nothing else matches. Then since we always want to return the display name if we have it do just that.
EDIT: Tweak to prevent returning "NULL "
使用三元条件我们可以缩短和美化代码:
Using ternary conditions we can shorten and beautify the code:
我建议这样:
I would propose this:
虽然不多,但是因为 $name 它至少是 NULL:
It is not much, but because $name it is at least NULL:
可读性稍差,但有效):
Somewhat less readable, but effective):
状态机非常适合这样的复杂逻辑。实现起来也非常简单(使用 switch 语句)。
A State machine works very nicely for involved logic like that. It's very simple to implement as well (using a switch statement).
我不确定我的版本会更简单,但这里是:
I'm not sure that my version would be simplier, but here it is:
我会同意:
这将是“核心逻辑”...需要进行其他检查,例如
$names != NULL
或其他检查。I'd go with:
This would be the 'core logic'... there will need to be other checks like
$names != NULL
or something..