2017-09-16 117 views
-1

这是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'; 
?> 
+0

通过检查[文档](http://php.net/manual/en/language.oop5.decon.php)可以很容易地识别出这个错字。 – faintsignal

回答

0
public function __constructor($description) 

应该被命名为__construct代替。


下面

前面的答案,如果它仍然不能正常工作:

替换:

<?= $task->description; ?> 

由:

<?php echo $task->description; ?> 

,并做相同的另一种。或者在您的php ini文件中启用short_tags设置(并且optionnaly重新启动您的网络服务器)。

+0

没有事情发生我做了所有的事情,但错误仍然相同 –

+0

你有另一个问题。我会更新答案。 – Calimero

+0

那么问题是什么?你能解决它吗? –