PHP,致命错误:调用未定义的方法。但方法是定义的?

问题描述:

所以在这段代码中,我所要做的就是创建一个视图杂项页面。 我创建了一个chore类和一个choreDAO类,我的view_chores页面正在调用它。PHP,致命错误:调用未定义的方法。但方法是定义的?

我已经使用完全相同的代码与其他页面,如view_members,他们也有其他两个类,他们工作得很好!

该错误发生在下面的view_chores.php文件中;这行代码:

echo "<b>Title:</b> " . $chore->getChoreName() . "<br />"; 

任何帮助将是伟大的。谢谢!

这是我的view_chores.php页面。

<?php 
ob_start(); 
require_once 'includes/Member.php'; 
require_once 'includes/MemberDAO.php'; 
require_once 'includes/Chore.php'; 
require_once 'includes/ChoreDAO.php'; 
require_once 'includes/session.php'; 
confirm_logged_in(); // needs to come before any html because it does a redirect 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title></title> 
    </head> 
    <body> 
     <?php 
     require 'toolbar.php'; 
     $member = ($_SESSION['member']); 

     $choreDAO = new ChoreDAO(); 
     $chores = $choreDAO->getChores(); 


     echo "<p>Hello " . $member->getFN() . "</p>"; 
     echo "<p>These are the current chores: </p>"; 


     foreach ($chores as $chore) { 
      echo "<b>Title:</b> " . $chore->getChoreName() . "<br />"; 
      echo "</p>"; 


     } 


     echo $display; ?> 
     <a href="add_chore_form.php">Add Chore?</a> 



    </body> 
</html> 

<?php ob_flush(); ?> 

这里是我的Chore.php

<?php 
class Chore { 
private $id; 
private $chore_name; 



public function __construct($i, $chore_name) { 
    $this->id = $i; 
    $this->chore_name = $chore_name; 


} 
public function getId() { return $this->id; } 
public function getChoreName() { return $this->chore_name; } 



public function setId($i) { $this->id = $i; } 
public function setChoreName($cn) { $this->ChoreName = $cn; } 


} 
?> 

,这里是我的ChoreDAO.php

<?php 
    require_once 'DAO.php'; 

    class ChoreDAO extends DAO { 

    public function __construct() { 
     parent::__construct(); 
    } 

    public function insert($chore) { 
     if (!isset($chore)) { 
      throw new Exception("Chore required"); 
     } 
     $sql = "INSERT INTO Chore(chore_name) VALUES (?)"; 
     $params = array($chore->getChoreName()); 
     $stmt = $this->link->prepare($sql); 
     $status = $stmt->execute($params); 
     if ($status != true) { 
      $errorInfo = $stmt->errorInfo(); 
      throw new Exception("Could not save Chore: " . $errorInfo[2]); 
     } 

     $sql = "SELECT LAST_INSERT_ID()"; 
     $stmt = $this->link->prepare($sql); 
     $status = $stmt->execute(); 
     if ($status != true) { 
      $errorInfo = $stmt->errorInfo(); 
      throw new Exception("Could not retrieve new chore's id: " . $errorInfo[2]); 
     } 
     $row = $stmt->fetch(); 
     $id = $row[0]; 
     $chore->setId($id); 
    } 

    public function delete($chore) { 
     if (!isset($chore)) { 
      throw new Exception("Chore required"); 
     } 
     $id = $chore->getId(); 
     if ($id == null) { 
      throw new Exception("Chore id required"); 
     } 
     $sql = "DELETE FROM Chore WHERE id = ?"; 
     $params = array($chore->getId()); 
     $stmt = $this->link->prepare($sql); 
     $status = $stmt->execute($params); 
     if ($status != true) { 
      $errorInfo = $stmt->errorInfo(); 
      throw new Exception("Could not delete Chore: " . $errorInfo[2]); 
     } 
    } 

    public function update($chore) { 
     if (!isset($chore)) { 
      throw new Exception("Chore required"); 
     } 
     $id = $chore->getId(); 
     if ($id == null) { 
      throw new Exception("Chore id required"); 
     } 
     $sql = "UPDATE Chore SET chore_name = ? WHERE id = ?"; 
     $params = array($chore->getChoreName()); 
     $stmt = $this->link->prepare($sql); 
     $status = $stmt->execute($params); 
     if ($status != true) { 
      $errorInfo = $stmt->errorInfo(); 
      throw new Exception("Could not update Chore: " . $errorInfo[2]); 
     } 
    } 

    public function getChore($id) { 
     $sql = "SELECT * FROM Chore WHERE id = ?"; 
     $params = array($id); 
     $stmt = $this->link->prepare($sql); 
     $status = $stmt->execute($params); 
     if ($status != true) { 
      $errorInfo = $stmt->errorInfo(); 
      throw new Exception("Could not retrieve Chore: " . $errorInfo[2]); 
     } 

     $chore = null; 
     if ($stmt->rowCount == 1) { 
      $row = $stmt->fetch(); 
      $id = $row['id']; 
      $chore_name = $row['house_name']; 

      $chore = new ChoreDAO($id, $chore_name); 
     } 
     return $chore; 
    } 

    public function getChores() { 
     $sql = "SELECT * FROM Chore"; 
     $stmt = $this->link->prepare($sql); 
     $status = $stmt->execute(); 
     if ($status != true) { 
      $errorInfo = $stmt->errorInfo(); 
      throw new Exception("Could not retrieve chores: " . $errorInfo[2]); 
     } 

     $chores = array(); 
     $row = $stmt->fetch(); 
     while ($row != null) { 
      $id = $row['id']; 
      $chore_name = $row['chore_name']; 

      $chore= new ChoreDAO($i, $chore_name); 
      $chores[$id] = $chore; 

      $row = $stmt->fetch(); 
     } 
     return $chores; 
    } 
    } 
    ?> 
+0

这是未定义的方法? – Nick 2012-02-02 13:56:00

+0

对不起,在view_chores.php 这行代码: echo“标题:”。 $ chore-> getChoreName()。 “
”; – suzaane 2012-02-02 13:57:15

+0

请创建一个5-10行测试用例。你需要做更多的调试。 – 2012-02-02 14:07:04

在你ChoreDAO类getChores()方法中,使用的是这样的:

$chore= new ChoreDAO($i, $chore_name); 

,它应该是:

$chore= new Chore($i, $chore_name); 
+0

非常感谢,我已经整理出来了! 祝你有美好的一天! – suzaane 2012-02-02 14:10:55

getChoreName()存在于家务。 因此,Chore :: getChoreName()存在,但您没有使用该类。也许你想让ChoreDAO扩展Chore类?

+0

他把它叫做'家务'。 – 2012-02-02 14:07:26

+0

哦哇,我错过了,没有看到滚动条。 – Housni 2012-02-02 14:15:29

+0

我不完全确定什么是滚动条与任何事情有关,但可以。 – 2012-02-02 14:36:57

另外,看不应该苦差事:: setChoreName()

不应该$this->ChoreName$this->chore_name