Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.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_String_Parameters - Fatal编程技术网

强制在php中作为字符串参数给定的内容

强制在php中作为字符串参数给定的内容,php,string,parameters,Php,String,Parameters,我一直在寻找我问题的答案,但难以用语言表达。所以我在问你 我创建了一个方法,用于回显具有给定字符串和错误类型的文本: public function output($string, $errorType) : void { echo "<div class='alert alert-$errorType' role='alert'>$string</div>"; } 公共函数输出($string,$errorType):无效 { 回显“$string”; }

我一直在寻找我问题的答案,但难以用语言表达。所以我在问你

我创建了一个方法,用于回显具有给定字符串和错误类型的文本:

public function output($string, $errorType) : void
{
    echo "<div class='alert alert-$errorType' role='alert'>$string</div>";
}
公共函数输出($string,$errorType):无效
{
回显“$string”;
}
因为我使用的是引导,所以这个
$errorType
应该始终是警告、危险或成功。但是由于它是一个字符串,我可以给
$errorType
一个完全不同的值。我想强制使用三种错误类型:警告、危险或成功。执行此操作的最佳方法是什么?

如果
$errorType
的输入值无效。您可以执行以下操作:

public function output($string, $errorType) : void
{
    // define all possible valid input values for $errorType
    $accepted_error_type = array('warning', 'danger', 'success');

    // throw exception if invalid input
    if (!in_array($errorType, $accepted_error_type) {
        throw new Exception('Invalid errorType in function output. Expected: warning, danger or success. Received: ' . $errorType);
    }

    // Rest of the implementation details come here
    echo "<div class='alert alert-$errorType' role='alert'>$string</div>";
}
公共函数输出($string,$errorType):无效
{
//为$errorType定义所有可能的有效输入值
$accepted_error_type=数组('warning'、'danger'、'success');
//如果输入无效,则引发异常
如果(!in_数组($errorType,$accepted_error_type){
抛出新异常('函数输出中的错误类型无效。应为警告、危险或成功。收到:'。$errorType);
}
//其余的实现细节都在这里
回显“$string”;
}

您可以让用户以
$errorType
的形式给出int。例如,0表示警告,1表示危险,2表示成功。对于其他值,您可以设置默认值(例如警告),也可以返回异常

public function output($string, $errorType)
    {
       $type = "";

       if($errorType == 0)
          $type = "warning";

       else if($errorType == 1)
          $type = "danger";

       else if($errorType == 2)
          $type = "success";

       else
       {
         // You can set a default error type here, if input is incorrect
         // Or you can simply return an exception to inform user about limitations.
       }


        echo "<div class='alert alert-$type' role='alert'>$string</div>";
    }
公共函数输出($string,$errorType)
{
$type=“”;
如果($errorType==0)
$type=“警告”;
else if($errorType==1)
$type=“危险”;
else if($errorType==2)
$type=“成功”;
其他的
{
//如果输入不正确,可以在此处设置默认错误类型
//或者,您可以简单地返回一个异常以通知用户限制。
}
回显“$string”;
}

这个问题是输入验证问题之一

在您的上下文中,有几种方法可供选择。最终目标是检查输入是否在可接受值列表中,但主要问题是在何处、何时以及如何执行

最直观的方法是将它放在方法中,例如

if (!in_array($input, self::ACCEPTABLE_INPUT)) {
  throw some exception
}
本着同样的精神,另一种机制是使用断言:

assert(in_array($input, self::ACCEPTABLE_INPUT));
然而,这假设了一个良好的测试覆盖率

我通常倾向于选择的一种方法是将错误类型建模为值对象,例如

final class ErrorType
{
    private const ACCEPTED = ['warning', 'danger', 'success'];
    private $type;
    public function __construct(string $type)
    {
        if (!in_array($type, ErrorType::ACCEPTED) {
             throw some exception
        }
        $this->type = $type;
    }

    public function __toString(): string
    {
        return $this->type;
    }
}
这是更多的代码,但允许您遵循单一责任原则

您的函数将如下所示:

public function output($string, ErrorType $errorType) : void
{
    echo "<div class='alert alert-$errorType' role='alert'>$string</div>";
}
公共函数输出($string,ErrorType$ErrorType):无效
{
回显“$string”;
}

谢谢你的回答!你认为这是最好的解决方法吗?@user3478148这是软件开发的标准做法。编程语言通常不提供任何函数参数的隔离。这是解决问题的几种方法之一。每种解决方案都有优缺点,其优点是这一版本的时代是,它使验证接近使用情况,但它的缺点是,如果您在其他地方也需要相同的验证,它会破坏SRP,并可能导致大量代码重用。@Stratadox true!为每一件事情创建类是一个非常主观的决定。这也在很大程度上取决于项目规模。我想我该走了nna同意@Stratadox的答案。无论如何,我都会支持你的答案,因为这也是一个好的解决方案。好的答案!我的支持:)不过,将一切可视化为类确实取决于手头的问题规模。的确,每个解决方案都有其优点和缺点。“最佳”解决方案是相对于问题背景而言,正面比负面更重要。谢谢@Stratadox!关于“错误”类型成功的观点很好。@Stratadox一个问题。。。您正在构造函数中抛出一个错误。构造函数不只是为了声明访问修饰符而不是为了逻辑吗?访问修饰符是公共保护的和私有的。在构造函数中,通常只分配属性和保护不变量。当人们说构造器不适用于逻辑时,他们的意思是“业务逻辑”,例如,你不会影响其他对象或从那里发送电子邮件。验证逻辑是一个完全不同的问题,当然可以在构造函数中进行。谢谢你的回答。然而,看看其他答案,我觉得这不是实现我想要的最好的方式。