为什么我在PHP中有“解析错误:语法错误,意外的T_PUBLIC”?

时间:2013-11-08 14:28:38

标签: php

我正在使用PHP中的使用对象(Codeacademy.com)执行属性恐慌(2),但是我有这个错误。 这是我的代码:

 <!DOCTYPE html>
 <html>
<head>
  <title>Reconstructing the Person Class</title>
  <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
  <p>
  <?php

    class Person{
        public $isAlive = true;
        public $firstname;
        public $lastname;
        public $age;
    }



   public function __construct($firstname, $lastname, $age){
        $this->firstname = $firstname;
        $this->lastname = $lastname;
        $this->age = $age;
    }

    $teacher = new Person("boring","12345",12345);
    $student = new Person("a","b",23);

    echo $teacher->age;
    ?>

  </p>
</body>

它来自哪里?感谢

1 个答案:

答案 0 :(得分:14)

你的构造函数属于里面你的类:

class Person{
    public $isAlive = true;
    public $firstname;
    public $lastname;
    public $age;

    public function __construct($firstname, $lastname, $age){
        $this->firstname = $firstname;
        $this->lastname = $lastname;
        $this->age = $age;
    } 
}