我的代码的最后一行出了什么问题?

时间:2014-03-17 04:44:48

标签: php function oop error-handling

以下代码适用于我的大学项目。当我作为网页运行时,最后一行有错误...我需要有关如何解决此问题的帮助。

<?php
include_once 'ShapeClass.php';    
class Square extends Shape
      print "<h2>This square's side is not bigger than the parallelogram's height</h2>";
   }
break;
}
?>

4 个答案:

答案 0 :(得分:6)

正确的方式..

<?php
include_once 'ShapeClass.php';

class Square extends Shape
{
    function __construct()
    {
                print("<h2>This square's side is not bigger than the parallelogram's height</h2>");
    }
}

你犯的错误

  • 有一个不必要的break声明。
  • 您不能直接print,因为它在课堂内。你需要将它包装在一个方法中。
  • 您忘了在{
  • 之后打开class

答案 1 :(得分:0)

试试这个,     

class Square extends Shape {
     function __construct() {
            print "<h2>This square's side is not bigger than the parallelogram's   height</h2>";
 }

}
?>

答案 2 :(得分:0)

你可能想检查你的代码中的大括号,它看起来很随意。     

include_once 'ShapeClass.php';

class Square extends Shape
{
    print "&lt;h2>This square's side is not bigger than the parallelogram's height&lt;/h2>";
    break;
}

?>

答案 3 :(得分:0)

这有很多问题。首先,类代码块的格式为

class MyClass extends OtherClass {
    // code for MyClass goes here
}

你甚至没有开口大括号。

其次,不应该在类中放置任意代码。因此,print_r()不应直接放在花括号内。

第三,如果您尝试停止执行,break在上下文中不正确。由于它也是任意代码,因此不应将其直接放在花括号内。此外,它应该是exit()

我建议阅读Larry Ullman的书http://www.larryullman.com/books/php-advanced-and-object-oriented-programming/中关于面向对象编程的3章。此外,您可能会考虑阅读面向对象的php http://au2.php.net/oop5.basic的基础知识。