这是我的代码而且这个类搞砸了,所以它不会回应
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;
答案 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
一个对象。