返回布尔结果和状态消息的PHP函数/方法

4
我有一个包含方法的类,这些方法需要返回它们的结果状态(true|false),并返回一个状态消息(“它工作了/由于x无法工作...”)。
这是我尝试过的两种方法…
第一种方法:返回布尔值,并通过引用传递消息
函数示例:
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."
}

方法二:返回一个结果对象
函数示例:
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;
}

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

2
基于个人观点,但第二个例子是面向对象编程(OOP),更接近于PHP异常和其他功能的工作方式。特别是如果你的代码是面向对象的,那看起来最好。 - AbraCadaver
1
最好使用异常。 - user4035
@hindmost 然后你可以使用 list($success, $message) = do_something($arg1, $arg2) 进行解包,类似于 Python 的元组赋值。 - candu
1
你的第一个选项永远不会出现“失败原因”,因为你的函数从不返回false。它是硬编码为返回true的,所以即使消息是失败的,你仍然表明成功。 - Marc B
如果你可以使用数组来“编码”东西,而不需要编码/解码的开销,为什么还需要JSON呢? - Paul
显示剩余3条评论
2个回答

3
我建议返回一个关联数组,其中包含两个元素:
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'];
}

如果你需要在代码的许多模块中使用这些结果值,我会选择第二种方法“返回一个结果对象”,因为使用这种方法可以更好地封装数据。

个人意见: 我肯定不会在 PHP 中使用引用,我感觉这种方式在这种语言中用处不大。


好的 - 我确实需要通过各种模块来完成,所以我会选择第二种方法。谢谢。 - Camden S.

0

如果你想真正地实现面向对象编程,这是你应该使用的方法。

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());

我不会把使用“static”限定为面向对象编程,因为它们几乎类似于全局变量,而且还有更优雅的解决方案和更符合面向对象编程的方法(例如:异常)。 - Paul
@Paul,在这里异常并不是很有意义,因为这些不仅仅是“错误”,但你有什么建议? - Camden S.
1
@CamdenS。是的,在这种情况下,OP也想报告成功状态,这就是为什么我在我的答案中没有提到异常的原因。上面的评论只是想提醒Adri1du40,异常是更好的错误报告方式(他在回答中只涉及了错误报告的情况,没有涉及成功状态)。也许我有点过分了? - Paul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接