这是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