0

运行我的代码时出现错误,它说调用非对象上的成员函数 getBallparkDetailsS​​tartDate()。

if($projectStatusId == ProjectStatusKeys::BALLPARK_ACTIVE) {
            $ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);               
            $projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
            $projectDetails["enddate"] = $ballpark->getBallparkDetailsEndDate();
            $projectDetails["projectid"] = $projectId;
            $projectDetails["name"] = $ballpark->getBallparkDetailsBookingRef();
            $projectDetails["status"] = ProjectStatusKeys::BALLPARK_ACTIVE; 
        }

我在这一行得到了错误:$projectDetails["startdate"] = $ballpark->getBallparkDetailsS​​tartDate();

这是我的其他代码:

public function __construct($ballparkDetailsId, $project, 
        $ballparkDetailsBookingRef, 
        $ballparkDetailsStartDate, $ballparkDetailsEndDate, 
        $ballparkDetailsExpiryDate, $ballparkDetailsDescription, 
        $ballparkDetailsNotes) {
    $this->ballparkDetailsId = $ballparkDetailsId;
    $this->project = $project;
    $this->ballparkDetailsBookingRef = $ballparkDetailsBookingRef;
    $this->ballparkDetailsStartDate = $ballparkDetailsStartDate;
    $this->ballparkDetailsEndDate = $ballparkDetailsEndDate;
    $this->ballparkDetailsExpiryDate = $ballparkDetailsExpiryDate;
    $this->ballparkDetailsDescription = $ballparkDetailsDescription;
    $this->ballparkDetailsNotes = $ballparkDetailsNotes;
}

public function getBallparkDetailsId() {
    return $this->ballparkDetailsId;
}

public function getProject() {
    return $this->project;
}

public function getBankName() {
    return $this->getProject()->getBankName();
}

public function getBankRef() {
    return $this->getProject()->getBankRef();
}

public function getRegionName() {
    return $this->getProject()->getRegionName();
}

public function getProjectStatusName() {
    return $this->getProject()->getProjectStatusName();
}

public function getBallparkDetailsBookingRef() {
    return $this->ballparkDetailsBookingRef;
}

public function getBallparkDetailsStartDate() {
    return $this->ballparkDetailsStartDate;
}

public function getBallparkDetailsEndDate() {
    return $this->ballparkDetailsEndDate;
}

public function getBallparkDetailsExpiryDate() {
    return $this->ballparkDetailsExpiryDate;
}

public function getBallparkDetailsDescription() {
    return $this->ballparkDetailsDescription;
}

public function getBallparkDetailsNotes() {
    return $this->ballparkDetailsNotes;
}

public function getProjectId() {
    return $this->getProject()->getProjectId();
}

public function getProjectStatusId() {
    return $this->getProject()->getProjectStatusId();
}

}
?>

我最后一次检查它运行良好。但是现在我不知道这是怎么回事?请帮我找出错误。谢谢。

4

2 回答 2

1

显然

$ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);

根本没有返回“球场”。可能它正在返回一个错误,或者类似一个空数组。

在引发错误的行之前尝试var_dump()'ing ,并查看它包含的内容(可能是 ,或其他类似的东西。$ballparkFalseNULLarray()

然后,检查文件ballparkDetailsByProjectId()中的函数BallparkDetailsHandler.php。猜测一下,您可能传递了一个无效的(即不存在、已删除等)$projectId

然后你可以用错误检查重写代码:

if($projectStatusId == ProjectStatusKeys::BALLPARK_ACTIVE) {
        $ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);
        if (!is_object($ballpark))
            trigger_error("Error: bad project ID: '$projectId': $ballpark",
                E_USER_ERROR);

        $projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
        $projectDetails["enddate"] = $ballpark->getBallparkDetailsEndDate();
        $projectDetails["projectid"] = $projectId;
        $projectDetails["name"] = $ballpark->getBallparkDetailsBookingRef();
        $projectDetails["status"] = ProjectStatusKeys::BALLPARK_ACTIVE; 
    }

然后在BallparkDetailsHandler.php文件中您可以修改此代码:

// Prepare query or die
if (!($stmt = $this->mysqli->prepare($query))
    return "Error in PREPARE: $query";

$stmt->bind_param("i", $projectId);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ballparkDetailsBookingRef, $bankRef, $regionName,
     $projectStatusId, $projectStatusName,  $ballparkDetailsDescription,
     $ballparkDetailsNotes, $ballparkDetailsStartDate, $ballparkDetailsEndDate,
     $ballparkDetailsExpiryDate);
$stmt->fetch();

// If no data, then die
if(!$stmt->num_rows)
    return "No data in DB for projectID '$projectId': $query";

// Should be clear sailing from here on. Actually I ought to check
// whether all these new() here do return anything sensible, or not

$bank = new Bank("", "", $bankRef, "");
$region = new Region("", $regionName, "");
$projectStatus = new ProjectStatus($projectStatusId, $projectStatusName);
$project = new Project($projectId, $bank, $region, $projectStatus);

return new BallparkDetails("", $project,
    $ballparkDetailsBookingRef, $ballparkDetailsStartDate, 
    $ballparkDetailsEndDate, $ballparkDetailsExpiryDate, 
    $ballparkDetailsDescription, $ballparkDetailsNotes);
于 2012-10-16T11:36:27.507 回答
0

$ballpark显然不包含您认为与错误有关的对象。事实上,它显然根本不包含一个对象。

这意味着前面的行(设置$ballpark)没有正常工作。看起来它返回的值不是对象。

我不知道那个值是什么——它可能是null,也可能是整数、字符串、数组等。但不管它是什么,它都不是一个球场对象。

我建议你看看你的getBallparkDetailsByProjectId()方法来找到这个问题的根源。

于 2012-10-16T11:39:02.067 回答