PHP类输出正确但运动说错了

时间:2017-01-03 11:00:24

标签: php

以下代码输出以下短语:

你好,我的名字是无聊的12345.很高兴认识你! : - )

您好,我的名字是Sasha Chirico。很高兴见到你! : - )

它看起来不错,但是当我点击" Save&提交代码"这个告诉我:"嘿!你忘记了把我的名字作为对象的推荐吗?"

出了什么问题?

<!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;
    }
    public function greet(){
        return "Hello, my name is ".$this->firstname." ".$this->lastname.". Nice to meet you! :-)";
    }
}

$teacher = new Person("Boring", "12345", 12345);
$student = new Person("Sasha", "Chirico", 22);
echo $teacher->greet()."<br/><br/>";
echo $student->greet();

        ?>
</p>
</body>
</html>

screenshot-codecademy

1 个答案:

答案 0 :(得分:1)

<!DOCTYPE html>
<html>
<head>
<title>Reconstructing the Person Class</title>
<link type='text/css' rel='stylesheet' href='style.css'/>

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



            public function greet() {
                return "Hello, my name is $this->firstname $this->lastname Nice to "
                        . " meet you";
            }

        }

        $teacher = new Person("boring", "12345", 12345);
        $student = new Person("Sasha", "Chirico", 22);

        echo $student->greet();
        echo $teacher->greet();

        ?>

</p>
</body>
</html>