2017-09-16 112 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

那麼問題是什麼?你能解決它嗎? –