【发布时间】:2014-09-16 14:04:00
【问题描述】:
这是我的代码:
<!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() {
$this->firstname = "Umair";
$this->lastname = "Dongle";
$this->age = 23;
}
}
$teacher = new Person("Matt","Zinger",34);
$student = new Person("Hassan", "Naseer", 90);
echo $teacher->age;
?>
</p>
</body>
</html>
这个结果应该是 34,但我得到了 23。有人可以解释一下,因为我是 PHP 新手,还在学习。如果 -> 这个有线的东西有另一种语法,那么也请告诉我。 谢谢
【问题讨论】:
-
你的构造器上没有参数
-
每次构建新对象时,您都会为变量分配相同的硬编码值。
标签: php class oop constructor