這是PHP的主要文件,foreach循環將顯示類的對象任務但它並不顯示那些 這是index.view.php文件Php類對象不顯示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<ul>
<?php foreach ($tasks as $task) : ?>
<li>
<?php if($task->completed) : ?>
<strike><?= $task->description; ?></strike>
<?php else : ?>
<?= $task->description; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
這是index.php文件,其中類存在,也是類的對象在這裏。所有的方法也在這裏,但我不知道什麼是錯誤,爲什麼我的對象不顯示。 唯一顯示的是三行中的子彈作爲(li)標籤的功能,但是這些對象不顯示。
<?php
class Task
{
public $description;
public $completed = false;
public function __constructor($description)
{
$this->description = $description;
}
public function complete()
{
$this->completed = true;
}
public function Iscomplete()
{
return $this->completed;
}
}
$tasks =
[
new Task('Go to the store'),
new Task('Finished the work'),
new Task('Cleaned the room')
];
$tasks[0]->complete();
require 'index.view.php';
?>
通過檢查[文檔](http://php.net/manual/en/language.oop5.decon.php)可以很容易地識別出這個錯字。 – faintsignal