【问题标题】:PHP function/method returning boolean result and a status messagePHP 函数/方法返回布尔结果和状态消息
【发布时间】:2014-06-18 07:25:03
【问题描述】:

我有一个类,其方法需要返回结果状态 (true|false) 并返回状态消息(“它工作/没有工作,因为 x...”)。

这是我尝试过的两种方法...

方法#1:返回布尔值并通过引用传递消息

函数示例:

function do_something ($arg1, $arg2, &$message) {

  ... do stuff resulting success...

  // Give an explanation for why it succeeded... reasons could be varied:
  $message = 'It succeeded and here are your instructions for celebrating: ...';
  $success = true;

  ... do stuff resulting in failure...

  // Give an explanation for why it failed... reasons could be varied:
  $message = 'it failed because of so and so...';
  $success = false;

  return $success;
}

调用示例:

$message = '';
if ( do_something($arg1, $arg2, $message) ) {
  echo "It succeeded because $message.";
} else {
  echo "It failed because $message."
}

方法 #2:返回一个 Result 对象

函数示例:

function do_something ($arg1, $arg2) {

  ... do stuff...

  // Give an explanation for why it succeeded... reasons could be varied:
  $message = 'It succeeded and here are your instructions for celebrating: ...';
  $success = true;

  ... do stuff...

  // Give an explanation for why it failed... reasons could be varied:
  $message = 'it failed because of so and so...';
  $success = false;

  return new Result($success, $message);
}

你可以想象Result的类定义会是什么样子,所以我就不举例了。

调用示例:

$message = '';
$DoSomething = do_something($arg1, $arg2, $message);
if ( $DoSomething->success ) {
  echo "It succeeded because ". $DoSomething->message;
} else {
  echo "It failed because ". $DoSomething->message;
}

什么是最好的方法,为什么?

【问题讨论】:

  • 你也可以使用return array($success, $message);
  • 基于意见,但第二个例子是 OOP,更接近 PHP 异常和其他事情的工作方式。特别是如果您的代码是 OOP,那看起来最好。
  • 更好的使用例外
  • @hindmost 然后你可以用list($success, $message) = do_something($arg1, $arg2)解包,类似于Python的元组赋值。
  • 您的第一个选项永远不会执行“它失败了,因为”,因为您的函数永远不会返回 false。它被硬编码为返回 true,所以即使消息是失败的,你仍然表示成功。

标签: php oop design-patterns return pass-by-reference


【解决方案1】:

我会返回一个包含两个元素的关联数组:

return array('result' => true, 'message' => 'The operation executed fine!')

return array('result' => false, 'message' => 'The operation failed because...')

这样客户端代码会以这种方式访问​​值:

$retval = do_something();
if($retval['result']){
    //...
}
else{
echo 'Ooups: ', $retval['message'];
}

或者,如果您在代码的许多模块中都需要这些结果值,我会使用方法#2“返回结果对象”,因为通过使用这种方法,数据更加封装。

个人意见: 我绝对不会在 PHP 中使用引用,我只是在这种语言中感觉不到它们。

【讨论】:

  • 好的 - 我确实需要它通过各种模块,所以我将使用方法 #2。谢谢。
【解决方案2】:

如果你想真正做 OOP,这里就是你应该使用的

class Test
{
    private static $err;

    public static function do_something ($arg1, $arg2)
    {           
        $bool = rand(0,1); //Just for testing

        self::$err = $bool ? 'It succeeded and here are your instructions for celebrating: ...' : 'it failed because of so and so...';

        return $bool;
    }

    public static function get_error ()
    {
        return self::$err;
    }
}

Test::do_something(0, 0);
printf(Test::get_error());

【讨论】:

  • 我不完全符合将statics 用作 OOP 的条件,因为它们非常类似于全局变量,还有更优雅的解决方案和更多 OOP 风格(例如:异常)。
  • @Paul,例外在这里没有意义,因为这些不仅仅是“错误”,但你有什么建议?
  • @CamdenS。是的,在这种情况下,OP 也想报告成功状态,这就是为什么我没有在回答中提到异常。上述评论只是为了提醒 Adri1du40,异常是一种更好的错误报告方式(这是他在回答中涵盖的唯一情况,未涵盖成功状态)。也许我有点过激?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 2018-06-30
  • 2013-11-24
  • 2016-09-18
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多