php oop一些模糊的东西

时间:2016-06-07 00:17:33

标签: php oop object

非常快速的php对象问题。所以这是代码:

<?php
            class Person {
            public $isAlive = true;
            public $firstname, $lastname, $age;

       public function __construct($firstname, $lastname, $age) {
           $this->firstname = $firstname;
           $this->lastname = $lastname;
           $this->age = $age;
       }
            }

        $teacher = new Person("boring", "12345", "boring");
            echo $teacher->isAlive; 
        $student = new Person("Phil", "Kroupoderov", "20");
            echo $student->age;


         ?>

如果我回应我应该显示20岁的年龄,那么由于某种原因它显示为120。为什么有1以及如何摆脱它?????

1 个答案:

答案 0 :(得分:0)

John Conde是对的。你需要改变这个:

    $teacher = new Person("boring", "12345", "boring");
        echo $teacher->isAlive; 
    $student = new Person("Phil", "Kroupoderov", "20");
        echo $student->age;

到此:

    $teacher = new Person("boring", "12345", "boring");
        echo $teacher->isAlive."\n"; 
    $student = new Person("Phil", "Kroupoderov", "20");
        echo $student->age."\n";