Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回布尔结果和状态消息的PHP函数/方法_Php_Oop_Design Patterns_Return_Pass By Reference - Fatal编程技术网

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

返回布尔结果和状态消息的PHP函数/方法,php,oop,design-patterns,return,pass-by-reference,Php,Oop,Design Patterns,Return,Pass By Reference,我有一个类,其中的方法需要返回其结果状态(true | false),还需要返回一条状态消息(“它工作了/没有工作,因为x…”) 以下是我尝试过的两种方法 方法#1:返回布尔值并通过引用传递消息 功能示例: function do_something ($arg1, $arg2, &$message) { ... do stuff resulting success... // Give an explanation for why it succeeded... reaso

我有一个类,其中的方法需要返回其结果状态(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;
}
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);
}
呼叫示例:

$message = '';
if ( do_something($arg1, $arg2, $message) ) {
  echo "It succeeded because $message.";
} else {
  echo "It failed because $message."
}
$message = '';
$DoSomething = do_something($arg1, $arg2, $message);
if ( $DoSomething->success ) {
  echo "It succeeded because ". $DoSomething->message;
} else {
  echo "It failed because ". $DoSomething->message;
}
方法#2:返回结果对象

功能示例:

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;
}
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 = '';
if ( do_something($arg1, $arg2, $message) ) {
  echo "It succeeded because $message.";
} else {
  echo "It failed because $message."
}
$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('result' => true, 'message' => 'The operation executed fine!')

这样,客户端代码将通过以下方式访问值:

$retval = do_something();
if($retval['result']){
    //...
}
else{
echo 'Ooups: ', $retval['message'];
}
或者,如果在代码的许多模块中都需要这些结果值,我会使用方法#2“返回一个结果对象”,因为通过使用这种方法,数据更加封装

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

如果你真的想做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());

您可以使用
返回数组($success,$message)
也是基于观点的,但第二个示例是OOP,更接近PHP异常和其他事物的工作方式。特别是如果你的代码是面向对象的,那看起来最好。更好的使用exceptions@hindmost然后您可以使用
list($success,$message)=do_something($arg1,$arg2)
将其解包,类似于Python元组赋值。您的第一个选项永远不会执行“it failed because”,因为您的函数永远不会返回false。它被硬编码为返回true,因此即使消息是失败的,您仍然表示成功。我不能确切地将
static
s用作OOP,因为它们非常类似于全局变量,也有更优雅的解决方案和更多OOP(例如:例外)。@Paul,例外在这里没有真正的意义,因为这些不仅仅是“错误”,但你会建议什么?@CamdenS。是的,在这种情况下,OP也想报告成功状态,这就是为什么我在回答中没有提到异常。上述评论仅用于提醒Adri1du40,例外情况是错误报告的更好方式(这是他在回答中提到的唯一情况,未涵盖成功状态)。也许我有点过激?好吧-我确实需要通过不同的模块,所以我将使用方法2。谢谢