php类错误,它不会回应老师

时间:2014-08-07 17:44:49

标签: php

这是我的代码而且这个类搞砸了,所以它不会回应

class Person  __construct($firstname,$lastname,$age)
{
    public $isAlive = true;
    $firstname->prop1 = $firstname;
    $lastname->prop2 = $lastname;
    $age->prop3 = $age;
 }
 $teacher = new Person("boring", "12345", 12345);
 $student = new Person("boringw", "12345w", 12345);
 echo $teacher;

1 个答案:

答案 0 :(得分:2)

正如人们在评论中提到的,您的代码充满了各种错误。我相信你想要的是:

class Person {
   function __construct($firstname,$lastname,$age) {
       $this->isAlive   = true;
       $this->firstname = $firstname;
       $this->lastname  = $lastname;
       $this->age       = $age;
    }
}

$teacher = new Person("boring", "12345", 12345);
$student = new Person("boringw", "12345w", 12345);
print_r($teacher);

此代码已经过测试且有效。

结果:

人物对象([isAlive] => 1 [firstname] =>无聊[姓氏] => 12345 [年龄] => 12345)

注意:您不能echo一个对象。