PHP对象不会回显属性

时间:2013-07-05 12:54:02

标签: php oop object

我正在尝试在codeacademy上学习PHP OOP,我想我会疯了。 我已经将我的代码与示例代码进行了比较,并且它不会工作! 当我尝试从对象$ student中回显age属性时,请帮助我理解这里出了什么问题,这是由类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 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:1)

这是什么?

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

检查拼写costruct - 必须是construct

答案 2 :(得分:0)

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

尝试更换此行:public function __construct($firstname, $lastname, $age)

祝PHP学习好运。

答案 3 :(得分: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);