0

我正在尝试在 codeacademy 上学习 PHP OOP,我想我快疯了。我已经以各种方式将我的代码与示例代码进行了比较,但它就是行不通!请帮助我了解这里出了什么问题,当我尝试从对象 $student 中回显年龄属性时,该对象由 Person 类制成

  <?php
    class Person {
        public $isAlive = true;
        public $firstname;
        public $lastname;
        public $age;
        public function __contruct($firstname, $lastname, $age) 
        {
           $this->firstname = $firstname;
           $this->lastname = $lastname;
           $this->age = $age;
       }
   }
   $teacher = new Person("boring", "12345", 12345);
   $student = new Person('hans', 'hansen', 24);
   $me = new Person('boring', '12345', 12345);
   echo $student->age;
  ?>
4

4 回答 4

7

你拼错了'construct',所以没有设置任何东西。

于 2013-07-05T12:59:10.123 回答
1

这是什么 ?

public function __costruct($firstname, $lastname, $age) 

检查拼写costruct- 它必须是construct

于 2013-07-05T13:00:03.497 回答
0

这是一个语法错误......你写的__contruct

尝试替换这一行:public function __construct($firstname, $lastname, $age)

祝你学习 PHP 好运。

于 2013-07-05T12:59:29.260 回答
0

存在拼写错误

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

    //spelling mistake exist
    public function __construct($firstname, $lastname, $age)
    {
        echo $firstname;

        $this->firstname = $firstname;
        $this->lastname = $lastname;
        $this->age = $age;

        echo 'i work';
    }

}
$teacher = new Person("boring", "12345", 12345);
$student = new Person('hans', 'hansen', 24);

//print_r($student);

if(is_object($student))
{
    echo $student->age;
}

$me = new Person('boring', '12345', 12345);
于 2013-07-05T13:01:30.893 回答