2

我正在从在 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>

它从何而来?谢谢

4

1 回答 1

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;
    } 
}
于 2013-11-08T14:29:33.043 回答