PHP:致命错误:在非对象上调用成员函数get()

时间:2012-10-16 11:22:49

标签: php

我在运行代码时遇到错误,它在非对象上调用成员函数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();
}

}
?>

我最后一次检查它运行良好。但现在我不知道这有什么问题?请帮我找错。感谢。

2 个答案:

答案 0 :(得分:1)

显然

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

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

在引发错误的行之前立即尝试var_dump()'$ballpark,并查看其中包含的内容(可能是FalseNULLarray()或同样没有球的东西。

然后,检查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);

答案 1 :(得分:0)

$ballpark显然不包含您认为在错误行上执行的对象。事实上,它显然根本不包含任何对象。

这意味着前一行(设置$ballpark)无法正常工作。它似乎正在返回一个不是对象的值。

我无法分辨那个值是什么 - 它可能是null,或者它可能是整数,字符串,数组等等。但不管它是什么,它都不是一个棒球场。< / p>

我建议您查看getBallparkDetailsByProjectId()方法以找到此问题的根源。