在 try 块后 PHP 不执行

我创建了一个演示计算器,它完美运行,除了有一次提交表单时它进入 try 块,其余的 html 页面没有显示..我做错了吗?我看不出它不应该执行其余代码的任何理由


我非常理解 try catch finally 概念,但看不到错误在哪里


这是我的 class.calculator.php


class NoNumberProvided_Exception extends Exception {}


class Calculator {


    function __construct() {

        $args = func_get_args();


        if(!$args) {


            throw new NoNumberProvided_Exception("Please provide atleast 2 numbers");


        } else {


            if($args[0] && $args[1]) {


                if($args[2] == "Add") {


                    echo $args[0]+$args[1];


                } else if($args[2] == "Divide") { 


                    echo $args[0]/$args[1];


                } else if($args[2] == "Subtract") { 


                    echo $args[0]-$args[1];


                } else if($args[2] == "Multiply") { 


                    echo $args[0]*$args[1];


                }   


            } else {


                throw new NoNumberProvided_Exception("Please provide atleast 2 numbers");


            }


        }


    }


}

PHP:


if(isset($_POST['submit'])) {


    include 'class.calculator.php';


    try {


        $num = new Calculator($_POST['number1'], $_POST['number2'], $_POST['submit']);


        echo $num; // after the form gets submitted, this gets echoed but the html form below doesnt show on the page


    } catch (NoNumberProvided_Exception $nonumber) {


        echo $nonumber->getMessage();


    } 


}

HTML:


<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">


    Number1: <input type="text" name="number1" id="number1" />

    <br/>


    Number2: <input type="text" name="number2" id="number2" />

    <br/><br/>


    <input type="submit" id="submit" name="submit" value="Add" />


    <input type="submit" id="submit" name="submit" value="Divide" />


    <input type="submit" id="submit" name="submit" value="Subtract" />


    <input type="submit" id="submit" name="submit" value="Multiply" />


</form>


蝴蝶不菲
浏览 213回答 1
1回答

慕尼黑8549860

您之后没有看到 HTML 内容的原因echo $num是因为您遇到了致命错误。在 php 控制台 (php -a) 中运行代码时会看到这一点php > $num = new Calculator(1,2,'Subtract');-1php > echo $num;PHP Catchable fatal error:&nbsp; Object of class Calculator could not be converted to string in php shell code on line 1PHP Stack trace:PHP&nbsp; &nbsp;1. {main}() php shell code:0php >为了解决这个问题,我们可以对您的 Calculator 类进行一些概念上的更改:一个方法应该只做一件事。构造函数最适合用于初始化,您的计算器的功能Add,Subtract,Multiply,和Divide大概应该是独立的方法。作为一般规则,对象(作为模型或逻辑部分的一部分)不应该echo或print它的结果。在我看来,这是逻辑和表示的合并——您应该努力将其分开。这也是您遇到的问题的一部分。我的第一个倾向是将参数传递给方法,而不是构造函数。这允许您输入提示您期望的值。我以整数为例。如果传递了错误数据(字符串),则会抛出异常。如果只传递 1 个值,则会抛出异常。归根结底,没有一种“正确”的方法可以做到;经验有助于展示以后更容易处理的方法。:)我的建议,FWIW...班级:<?phpclass Calculator {&nbsp; public function __construct() { }&nbsp; public function add(int $addend1, int $addend2) {&nbsp; &nbsp; return $addend1 + $addend2;&nbsp; }&nbsp; public function subtract(int $subtrahend, int $minuend) {&nbsp; &nbsp; return $subtrahend - $minuend;&nbsp; }&nbsp; public function multiply(int $factor1, int $factor2) {&nbsp; &nbsp; return $factor1 * $factor2;&nbsp; }&nbsp; public function divide(int $dividend, int $divisor) {&nbsp; &nbsp; return $dividend / $divisor;&nbsp; }}PHP/HTMLfunction assignPostVar($name) {&nbsp; if(isset($_POST[$name])) {&nbsp; &nbsp; return $_POST[$name];&nbsp;&nbsp; }}$error&nbsp; = '';if(isset($_POST['submit'])) {&nbsp; $calc&nbsp; &nbsp;= new Calculator;&nbsp; $n1&nbsp; &nbsp; &nbsp;= assignPostVar('number1');&nbsp; $n2&nbsp; &nbsp; &nbsp;= assignPostVar('number2');&nbsp; try {&nbsp; &nbsp; switch($_POST['submit']) {&nbsp; &nbsp; &nbsp; case 'Add':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $result = $calc->add($n1,$n2);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 'Subtract':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $result = $calc->subtract($n1,$n2);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 'Multiply':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $result = $calc->multiply($n1,$n2);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 'Divide':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $result = $calc->divide($n1,$n2);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; throw new Exception('Invalid operation');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; } catch (Exception $e) {&nbsp; &nbsp; $error = $e->getMessage();&nbsp; }}?><?php if($error): ?>&nbsp; <div class="error">Encountered error: <?= $error ?></div><?php endif; ?><form method="POST">Number1: <input type="text" name="number1" id="number1" /><br/>Number2: <input type="text" name="number2" id="number2" /><br/><br/><input type="submit" id="add" name="submit" value="Add" /><input type="submit" id="divide" name="submit" value="Divide" /><input type="submit" id="subtract" name="submit" value="Subtract" /><input type="submit" id="multiply" name="submit" value="Multiply" /></form>
打开App,查看更多内容
随时随地看视频慕课网APP