0

我有一个名为 ResetPassword 的 PHP 类。在这个类中有一个开关,它将返回一个包含所需 HTML 的不同字符串。

这是下面的代码(为简洁起见,我省略了一些情况,还有一个返回 NULL 的默认情况)

    public function get_display_block($displayblock) {
     switch ($displayblock) {
    case 'EnterNewPassword':
        $displayblock = '
                        <form action="resetpassword.php" method="post">
                        <fieldset class="login">
                        <legend>Enter your information in the form below to reset your password: </legend>
                        <div><label for="password1">Password :</label>
                        <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                        <div><label for="password2"><span class="required">*</span>Retype Password :</label>
                        <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                        <br />
                        </fieldset><br />
                        <br />
                        <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                        </form>
                        ';
            return $displayblock;
        break;

    case 'THIS_ISNT_WORKING':
        $displayblock = '
                        <form action="changepassword.php" method="post">
                        <fieldset class="login">
                        <legend>Enter your information in the form below to reset your password: </legend>
                        <div><label for="password1">Old Password : </label>
                        <p><input type="password" name="oldpassword" size="40" maxlength="60" /></p> </div>
                        <div><label for="password1">New Password :</label>
                        <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                        <div><label for="password2"><span class="required">*</span>Retype New Password :</label>
                        <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                        <br />
                        </fieldset><br />
                        <br />
                        <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                        </form>
                        ';
        return $displayblock;
     break;
}  // end of switch

} //end of method

在我的主页上调用类/开关时,下面的代码工作正常。

$reset = new ResetPassword(); 
$displayblock = $reset->get_display_block('EnterSecretAnswer');

但是,当尝试调用 THIS_ISNT_WORKING 时,没有任何显示。$displayblock 上的 var_dump 返回 NULL。

$reset = new ResetPassword();
$displayblock = $reset->get_display_block('THIS_ISNT_WORKING');
var_dump($displayblock);

如果我调用任何其他情况(之前省略的情况),它会返回所需的 HTML。

谁能看到我在这里缺少的东西?像往常一样的任何帮助将不胜感激。

编辑:我已经按照下面的要求更新了开关,以便返回语句在开关之外。最后的结果还是一样的。我还更改了默认情况以返回一个字符串(“abcd”),只是为了验证默认情况没有被调用,它不是。仍然返回 NULL。

4

4 回答 4

1
public function get_display_block($displayblock) {
 switch ($displayblock) {
    case 'EnterNewPassword':
         $displayblock = '
                    <form action="resetpassword.php" method="post">
                    <fieldset class="login">
                    <legend>Enter your information in the form below to reset your password: </legend>
                    <div><label for="password1">Password :</label>
                    <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                    <div><label for="password2"><span class="required">*</span>Retype Password :</label>
                    <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                    <br />
                    </fieldset><br />
                    <br />
                    <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                    </form>
                    ';
    break;

   case 'THIS_ISNT_WORKING':
      $displayblock = '
                    <form action="changepassword.php" method="post">
                    <fieldset class="login">
                    <legend>Enter your information in the form below to reset your password: </legend>
                    <div><label for="password1">Old Password : </label>
                    <p><input type="password" name="oldpassword" size="40" maxlength="60" /></p> </div>
                    <div><label for="password1">New Password :</label>
                    <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                    <div><label for="password2"><span class="required">*</span>Retype New Password :</label>
                    <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                    <br />
                    </fieldset><br />
                    <br />
                    <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                    </form>
                    ';
         break;
    }

  return $displayblock;
}

更新

这是否有效:

   <?php

   $reset = new Reset;
   $displayblock = $reset->get_display_block('THIS_ISNT_WORKING');
   var_dump($displayblock);

   class Reset{
    public function get_display_block($displayblock) {
    switch ($displayblock) {
       case 'EnterNewPassword':
            $displayblock = '
                <form action="resetpassword.php" method="post">
                <fieldset class="login">
                <legend>Enter your information in the form below to reset your password: </legend>
                <div><label for="password1">Password :</label>
                <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                <div><label for="password2"><span class="required">*</span>Retype Password :</label>
                <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                <br />
                </fieldset><br />
                <br />
                <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                </form>
                ';
       break;

      case 'THIS_ISNT_WORKING':
         $displayblock = '
                <form action="changepassword.php" method="post">
                <fieldset class="login">
                <legend>Enter your information in the form below to reset your password: </legend>
                <div><label for="password1">Old Password : </label>
                <p><input type="password" name="oldpassword" size="40" maxlength="60" /></p> </div>
                <div><label for="password1">New Password :</label>
                <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                <div><label for="password2"><span class="required">*</span>Retype New Password :</label>
                <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                <br />
                </fieldset><br />
                <br />
                <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                </form>
                ';
            break;
       }

     return $displayblock;
   }


}

它为我返回一个非空响应。

于 2012-06-10T04:52:31.553 回答
1

你不应该return在 switch 中使用语句。据我所知,基本上return在函数中使用语句一次被称为一种好的做法。

于 2012-06-10T04:51:02.700 回答
0

我找到了解决方案,令人尴尬的是,错误不在开关或我调用它的方式中。我有一个配置文件,它包含在每个页面中(没有提及,因为认为它不相关),我忘记添加该类。

让我困惑这么久的事情是,我不会认为 switch 中的其他情况也可以工作,但他们确实......哦,好吧。

感谢所有回答的人。

于 2012-06-10T08:16:04.813 回答
-1

像这样使用它

function get_display_block($displayblock) {
    switch ($displayblock) {
        case 'EnterNewPassword':
            echo '<form action="resetpassword.php" method="post">' .
                 '<fieldset class="login">' .
                 '<legend>Enter your information in the form below to reset your password: </legend>' .
                 '<div><label for="password1">Password :</label>' .
                 '<p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>'.
                  '<div><label for="password2"><span class="required">*</span>Retype Password :</label>'.
                  '<p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>'.
                  '<br />'.
                  '</fieldset><br />'.
                  '<br />'.
                  '<div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>'.
                  '</form>';
            break;
      }
}

对你的其他情况也一样。虽然这样做可能有点草率,但你只需要通过调用在你的脚本上使用它<? get_displaying_block($yourString); ?>

于 2012-06-10T04:57:07.513 回答