PHP Return 策略。一种还是多种?
PHP Return strategy. One single type or more?
我构建了大型 php 应用程序,但我知道我在问自己这个问题:一个函数 return 应该只有一种数据类型并且为 null 还是 return 更多数据类型。因为不知何故现在开始感觉重新调整不止一种数据类型(null 除外)很脏,因为它邀请你在函数调用后写这样的东西:
if(is_array($returnVariable)) {
// do something
} else if(is_int($returnVarable)) {
// do something else
}
return 一个函数只做一件事或什么都不做似乎更符合逻辑,因为您不必到处乱扔 if 和 else 代码。但我可能完全不在了。以这个函数为例:
public function update(array $data, Model $user)
{
if(!is_a($user, User::class)) return null;
//Update the user
$user->fill($data);
//Send the user an activated notification if it was activated and it was not earlier.
if($user->getOriginal('activated') == 0 && $user->activated == 1) {
$user->activated_at = Carbon::now()->toDateTimeString();
$user->notify(new ActivatedUserNotification($user));
}
$saved = $user->save();
//User was not updated because of an error
if(!$saved) return null;
//User was successfully updated. Return the user
return $user;
}
我其实很想return一件事还是null。我这样做了。但是由于我 return 多个空值,我无法获得具体信息,为什么它在我调用该函数的地方 return 为空值。或者这可能表明该功能违反了关注点分离原则之类的东西?我也有其他类似的情况,所以请不要太具体地解决这个特定的功能。
对于return不止一种类型的功能,在PHP中是NORMAL。 strpos 就是一个例子。它 return 是一个整数或布尔值。您必须准备好区分整数零或布尔值 false。
仅 return 一种类型很有意义,如果在某些情况下该类型不能 return 则抛出异常。在其他情况下,return null
或那种类型更有意义。
如果您使用的是 PHP 7,您可以 declare a return type 这样...
function sum($a, $b): float {
return $a + $b;
}
这可以帮助您确保您的功能不会做太多事情,并且更容易使用您的 class'API。
虽然它确实有限制。没有 co/contra variance in PHP (yet) so there are situations (like an interface) which type hints will actually hinder the simplicity of your API.Additionally there are cases where you might want to use a generic 作为 return 类型。这些还不是 PHP 的一部分,因此如果您想使用泛型作为类型提示,您现在必须暂时没有该类型提示。
您似乎已经注意到使用 return 类型的一些好处,以及使用参数类型的好处。
键入参数的使用非常明显:它降低了函数的复杂性并使其倾向于做一件简洁的事情。
在我看来,使用 return 类型可以做同样的事情...但它并没有降低方法内部的复杂性,而是降低了执行该方法的代码的复杂性...这是可以说更重要,因为调用该方法的复杂代码将需要处理整个地方的变量 return 类型。
简单的例子...
function add($a, $b) {
if (is_string($a) && is_string($b)) {
return $a . $b;
} else if (is_int($a) && is_int($b)) {
return $a + $b;
}
}
这个函数没有类型参数,所以我们需要考虑它的常见用例并处理函数中的不同参数类型。它使我们的功能缺乏焦点,我们永远不会发现所有可能的情况,这意味着该功能可能会中断然后扩大范围。
我们也不知道 return 类型是什么,所以在使用该函数的任何地方我们都需要考虑...
$result = add($a, $b);
if (is_string($result)) {
// Do something
} else if (is_int($result)) {
// Do something else
}
我们可以解决这个问题:
function add(Int $a, Int $b) : Int {
return $a + $b;
}
function concatStrings(String $a, String $b) : String {
return $a . $b;
}
现在,无论我们在哪里使用这些函数,我们的类型检查逻辑都会消失。我们的功能更短、更简单,每个人都知道如何使用它们,以及它们应该做什么。测试它们也变得更容易。
我构建了大型 php 应用程序,但我知道我在问自己这个问题:一个函数 return 应该只有一种数据类型并且为 null 还是 return 更多数据类型。因为不知何故现在开始感觉重新调整不止一种数据类型(null 除外)很脏,因为它邀请你在函数调用后写这样的东西:
if(is_array($returnVariable)) {
// do something
} else if(is_int($returnVarable)) {
// do something else
}
return 一个函数只做一件事或什么都不做似乎更符合逻辑,因为您不必到处乱扔 if 和 else 代码。但我可能完全不在了。以这个函数为例:
public function update(array $data, Model $user)
{
if(!is_a($user, User::class)) return null;
//Update the user
$user->fill($data);
//Send the user an activated notification if it was activated and it was not earlier.
if($user->getOriginal('activated') == 0 && $user->activated == 1) {
$user->activated_at = Carbon::now()->toDateTimeString();
$user->notify(new ActivatedUserNotification($user));
}
$saved = $user->save();
//User was not updated because of an error
if(!$saved) return null;
//User was successfully updated. Return the user
return $user;
}
我其实很想return一件事还是null。我这样做了。但是由于我 return 多个空值,我无法获得具体信息,为什么它在我调用该函数的地方 return 为空值。或者这可能表明该功能违反了关注点分离原则之类的东西?我也有其他类似的情况,所以请不要太具体地解决这个特定的功能。
对于return不止一种类型的功能,在PHP中是NORMAL。 strpos 就是一个例子。它 return 是一个整数或布尔值。您必须准备好区分整数零或布尔值 false。
仅 return 一种类型很有意义,如果在某些情况下该类型不能 return 则抛出异常。在其他情况下,return null
或那种类型更有意义。
如果您使用的是 PHP 7,您可以 declare a return type 这样...
function sum($a, $b): float {
return $a + $b;
}
这可以帮助您确保您的功能不会做太多事情,并且更容易使用您的 class'API。
虽然它确实有限制。没有 co/contra variance in PHP (yet) so there are situations (like an interface) which type hints will actually hinder the simplicity of your API.Additionally there are cases where you might want to use a generic 作为 return 类型。这些还不是 PHP 的一部分,因此如果您想使用泛型作为类型提示,您现在必须暂时没有该类型提示。
您似乎已经注意到使用 return 类型的一些好处,以及使用参数类型的好处。
键入参数的使用非常明显:它降低了函数的复杂性并使其倾向于做一件简洁的事情。
在我看来,使用 return 类型可以做同样的事情...但它并没有降低方法内部的复杂性,而是降低了执行该方法的代码的复杂性...这是可以说更重要,因为调用该方法的复杂代码将需要处理整个地方的变量 return 类型。
简单的例子...
function add($a, $b) {
if (is_string($a) && is_string($b)) {
return $a . $b;
} else if (is_int($a) && is_int($b)) {
return $a + $b;
}
}
这个函数没有类型参数,所以我们需要考虑它的常见用例并处理函数中的不同参数类型。它使我们的功能缺乏焦点,我们永远不会发现所有可能的情况,这意味着该功能可能会中断然后扩大范围。
我们也不知道 return 类型是什么,所以在使用该函数的任何地方我们都需要考虑...
$result = add($a, $b);
if (is_string($result)) {
// Do something
} else if (is_int($result)) {
// Do something else
}
我们可以解决这个问题:
function add(Int $a, Int $b) : Int {
return $a + $b;
}
function concatStrings(String $a, String $b) : String {
return $a . $b;
}
现在,无论我们在哪里使用这些函数,我们的类型检查逻辑都会消失。我们的功能更短、更简单,每个人都知道如何使用它们,以及它们应该做什么。测试它们也变得更容易。