如何减少 PHP 中的代码行数?
How can I reduce lines of code in PHP?
我是一名网络开发人员,我在 PHP/Laravel 框架中编写代码。我一直在努力遵循编写代码的最佳实践,我知道在函数中编写 15-20 行代码和在 类 中最多编写 200 行代码是一个很好的做法。但每次我最终都会在一个函数中编写至少 40-50 行。例如,这里是我为获取客户和指定用户的详细信息而编写的代码片段。
public function preMessageSend($client, $assigned)
{
$ticket_number = $client->ticket_number;
$title = $client->title;
$department = $client->department;
$priority = $client->priority;
if ($client->first_name !== null || $client->first_name !== '') {
$client_name = $client->first_name." ".$client->last_name;
} else {
$client_name = $client->username;
}
if ($client->email !== '' || $client->email !== null) {
$client_email = $client->email;
} else {
$client->email = 'Not available';
}
if($client->mobile !== null || $client->mobile !== '') {
$client_mobile = $client->code."".$client->mobile;
} else {
$client_mobile = 'Not available';
}
if($assigned != null) {
if ($assigned->first_name !== null || $assigned->first_name !== '') {
$assigned_name = $assigned->first_name." ".$assigned->last_name;
} else {
$assigned_name = $assigned->username;
}
if ($assigned->email !== '' || $assigned->email !== null) {
$assigned_email = $assigned->email;
} else {
$assigned->email = 'Not available';
}
if($assigned->mobile !== null || $assigned->mobile !== '') {
$assigned_mobile = $assigned->code."".$assigned->mobile;
} else {
$assigned_mobile = 'Not available';
}
if ($assigned->address !== null || $assigned->address !== '') {
$assigned_address = $assigned->address;
} else {
$assigned_address = 'Not available';
}
$this->sendMessageWithAssigned($ticket_number, $title, $department, $priority, $client_name, $client_email, $client_mobile, $assigned_name, $assigned_email, $assigned_mobile, $assigned_address);
} else {
$this->sendMessageWithoutAssigned($ticket_number, $title, $department, $priority, $client_name, $client_email, $client_mobile);
}
请告诉我如何减少 class 和函数中的位置,以及避免编写如此长的函数的最佳做法是什么。
TIA
而不是
if ($client->first_name !== null || $client->first_name !== '') {
$client_name = $client->first_name." ".$client->last_name;
} else {
$client_name = $client->username;
}
你可以这样做:
$client_name = ($client->first_name !== null || $client->first_name !== '') ? $client->first_name." ".$client->last_name : $client->username;
首先,对于 empty()
,null
和 ''
是 true,所以你可以这样做:
if (!empty($client->first_name)) { // if not empty
$client_name = $client->first_name." ".$client->last_name;
} else {
$client_name = $client->username;
}
那么你也可以使用三元运算符:
$client_name = !empty($client->first_name) ? $client->first_name." ".$client->last_name : $client->username;
然后对于一些语句还有or
语句可用:
$client_email = $client->email or 'Not available';
$client_mobile = $client->code . $client->mobile or 'Not available';
$assigned_address = $assigned->address or 'Not available';
这些or
语句仅等于:
if(!empty($assigned->address)){
$assigned_address = $assigned->address;
} else {
$assigned_address = 'Not available';
}
// Or the equivalent ternary
$assigned_address = !empty($assigned->address) ? $assigned->address : 'Not available';
我对 "some" 的意思是:
$client->first_name = null;
$client->last_name = null;
echo empty($client->first_name." ".$client->last_name); // false
echo isset($client->first_name." ".$client->last_name); // true
不为空,即使两个变量都为空,因为 " "
space 会使它成为 isset()
现在要小心这些 or 语句,因为 !empty()
并不总是给出与 isset()
相反的结果,其中 isset([])
为真,而 empty([])
也为真。
正如其他人已经建议的那样,您可以使用 empty()
而不是 != null
和 != ''
检查。此外,您可以在大多数语句中省略 else
部分,例如:
$assigned_name = $assigned->username;
if (!empty($assigned->first_name)) {
$assigned_name = $assigned->first_name." ".$assigned->last_name;
}
默认情况下,这会将 $assigned_name
设置为您之前的 else
值,如果满足条件,$assigned_name
将被覆盖。我不建议使用三元运算符,因为它不是可读的 IMO。
只要代码可读且高效,我就不会太担心代码行。
我是一名网络开发人员,我在 PHP/Laravel 框架中编写代码。我一直在努力遵循编写代码的最佳实践,我知道在函数中编写 15-20 行代码和在 类 中最多编写 200 行代码是一个很好的做法。但每次我最终都会在一个函数中编写至少 40-50 行。例如,这里是我为获取客户和指定用户的详细信息而编写的代码片段。
public function preMessageSend($client, $assigned)
{
$ticket_number = $client->ticket_number;
$title = $client->title;
$department = $client->department;
$priority = $client->priority;
if ($client->first_name !== null || $client->first_name !== '') {
$client_name = $client->first_name." ".$client->last_name;
} else {
$client_name = $client->username;
}
if ($client->email !== '' || $client->email !== null) {
$client_email = $client->email;
} else {
$client->email = 'Not available';
}
if($client->mobile !== null || $client->mobile !== '') {
$client_mobile = $client->code."".$client->mobile;
} else {
$client_mobile = 'Not available';
}
if($assigned != null) {
if ($assigned->first_name !== null || $assigned->first_name !== '') {
$assigned_name = $assigned->first_name." ".$assigned->last_name;
} else {
$assigned_name = $assigned->username;
}
if ($assigned->email !== '' || $assigned->email !== null) {
$assigned_email = $assigned->email;
} else {
$assigned->email = 'Not available';
}
if($assigned->mobile !== null || $assigned->mobile !== '') {
$assigned_mobile = $assigned->code."".$assigned->mobile;
} else {
$assigned_mobile = 'Not available';
}
if ($assigned->address !== null || $assigned->address !== '') {
$assigned_address = $assigned->address;
} else {
$assigned_address = 'Not available';
}
$this->sendMessageWithAssigned($ticket_number, $title, $department, $priority, $client_name, $client_email, $client_mobile, $assigned_name, $assigned_email, $assigned_mobile, $assigned_address);
} else {
$this->sendMessageWithoutAssigned($ticket_number, $title, $department, $priority, $client_name, $client_email, $client_mobile);
}
请告诉我如何减少 class 和函数中的位置,以及避免编写如此长的函数的最佳做法是什么。 TIA
而不是
if ($client->first_name !== null || $client->first_name !== '') {
$client_name = $client->first_name." ".$client->last_name;
} else {
$client_name = $client->username;
}
你可以这样做:
$client_name = ($client->first_name !== null || $client->first_name !== '') ? $client->first_name." ".$client->last_name : $client->username;
首先,对于 empty()
,null
和 ''
是 true,所以你可以这样做:
if (!empty($client->first_name)) { // if not empty
$client_name = $client->first_name." ".$client->last_name;
} else {
$client_name = $client->username;
}
那么你也可以使用三元运算符:
$client_name = !empty($client->first_name) ? $client->first_name." ".$client->last_name : $client->username;
然后对于一些语句还有or
语句可用:
$client_email = $client->email or 'Not available';
$client_mobile = $client->code . $client->mobile or 'Not available';
$assigned_address = $assigned->address or 'Not available';
这些or
语句仅等于:
if(!empty($assigned->address)){
$assigned_address = $assigned->address;
} else {
$assigned_address = 'Not available';
}
// Or the equivalent ternary
$assigned_address = !empty($assigned->address) ? $assigned->address : 'Not available';
我对 "some" 的意思是:
$client->first_name = null;
$client->last_name = null;
echo empty($client->first_name." ".$client->last_name); // false
echo isset($client->first_name." ".$client->last_name); // true
不为空,即使两个变量都为空,因为 " "
space 会使它成为 isset()
现在要小心这些 or 语句,因为 !empty()
并不总是给出与 isset()
相反的结果,其中 isset([])
为真,而 empty([])
也为真。
正如其他人已经建议的那样,您可以使用 empty()
而不是 != null
和 != ''
检查。此外,您可以在大多数语句中省略 else
部分,例如:
$assigned_name = $assigned->username;
if (!empty($assigned->first_name)) {
$assigned_name = $assigned->first_name." ".$assigned->last_name;
}
默认情况下,这会将 $assigned_name
设置为您之前的 else
值,如果满足条件,$assigned_name
将被覆盖。我不建议使用三元运算符,因为它不是可读的 IMO。
只要代码可读且高效,我就不会太担心代码行。