gpt4 book ai didi

php - 为什么我在 PHP 中有一个 "Parse error: syntax error, unexpected T_PUBLIC"?

转载 作者:行者123 更新时间:2023-12-02 05:37:08 26 4
gpt4 key购买 nike

我正在执行在 PHP 中使用对象类(class) (Codecademy.com) 中的 Property Panic (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>

它从哪里来?谢谢

最佳答案

您的构造函数属于您的类内部:

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;
}
}

关于php - 为什么我在 PHP 中有一个 "Parse error: syntax error, unexpected T_PUBLIC"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19861413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com