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


PHP, Fatal error: Call to undefined method. But method is defined?

所以在这段代码中,我所要做的就是创建一个视图杂务页面。我创建了一个chore类和一个choreDAO类,我的view_chores页面就是从它们调用的。

我在其他页面(如view_members)中使用了完全相同的代码,它们还有另外两个classe,它们工作得很好!

错误出现在下面的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;
    }
   }
   ?>

在ChoreDAO类getChores()方法中,您使用的是:

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

应该在哪里:

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

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

另外,看看不应该Chore::setChoreName()

$this->ChoreName不应该是$this->chore_name吗?