我有一个名为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。
有人能看到我在这里失踪的东西吗? 像往常一样的任何帮助将不胜感激。
编辑:我已按照下面的要求更新了交换机,以便return语句在交换机之外。最终结果仍然相同。 我还更改了默认情况,返回一个字符串(“abcd”)只是为了验证没有调用默认情况,而且它不是。仍然返回NULL。
答案 0 :(得分:1)
你永远不应该在交换机中使用return
语句。根据我的知识,基本上在函数中使用return
语句被称为一种良好实践。
答案 1 :(得分: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;
}
}
它为我返回一个非空响应。
答案 2 :(得分:0)
我找到了解决方案,令人尴尬的是错误不在交换机或我调用它的方式。 我有一个配置文件,它包含在每个页面中(没有提及,因为没有认为它是相关的)我忘记添加该类。
让我困惑了很久的事情是,我不会想到开关中的其他情况也会起作用,但他们确实......好吧。
感谢所有回答的人。
答案 3 :(得分:-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); ?>