【问题标题】:Php error with the name of class on line 227第 227 行的类名称出现 PHP 错误
【发布时间】:2019-04-11 01:01:10
【问题描述】:

这段代码:

function CheckOrderStartCount($OrderID)
{
    global $pdo;
    global $layer;
    $apiskey = '';
    $stmt = $pdo->prepare('SELECT * FROM orders WHERE OrderID = :OrderID');
    $stmt->execute(array(
        ':OrderID' => $OrderID
    ));
    if ($stmt->rowCount() == 1) {
        $row             = $stmt->fetch();
        $start_count     = $row['OrderStartCount'];
        $ServiceOrderAPI = $layer->GetData('services', 'ServiceOrderAPI', 'ServiceID', $row['OrderServiceID']);
        if (empty($row['OrderStartCount']) && !empty($ServiceOrderAPI)) {
            $URL    = str_replace('[OrderID]', $row['OrderAPIID'], $ServiceOrderAPI);
            $URL = str_replace('[apiskey]', $apiskey, $URL);

            $return = $layer->SendCurl($URL);
            $resp   = json_decode($return);
            if (isset($resp) && property_exists($resp, 'start_count')) 
                $start_count = $resp->start_count;
            $stmt = $pdo->prepare('UPDATE orders SET OrderStartCount = :OrderStartCount WHERE OrderID = :OrderID');
            $stmt->execute(array(
                ':OrderStartCount' => $start_count,
                ':OrderID' => $OrderID
            )); 
        }
        return $start_count; 
    } else {
        return 0;
    }
}
public function DeclarePrice($ProductPrice, $ProductDefaultQuantity, $ProductQuantity)
{
    $ProductValue = $ProductPrice / $ProductDefaultQuantity;
    return $ProductValue * $ProductQuantity;
}

错误“第一个参数必须是对象或第 227 行上现有类的名称”: if (isset($resp) && property_exists($resp, 'start_count'))

如何解决这个问题?

【问题讨论】:

  • 那么,$resp 是什么,它是否适合该功能?您应该调试您的代码以找出并研究您正在使用的 API。相关的,如果不是答案:stackoverflow.com/q/14414379/1531971
  • 默认情况下 json_decode 返回一个对象或对象数组。问题是$resp 似乎既不是对象也不是类名,所以它一定是对象数组。对$resp 变量执行print_r 以查看其内容。

标签: php


【解决方案1】:
$resp   = json_decode($return);
if (isset($resp) && property_exists($resp, 'start_count'))

您收到的错误消息暗示 $resp 不是对象。阅读http://php.net/manual/en/function.json-decode.php 的文档后,您可能会注意到它写着Returns the value encoded in json in appropriate PHP type。那么你为什么会收到错误消息?

First parameter must either be an object or the name of an existing class on line 227

这一定是因为您的 json 不是对象,而是某种标量(int、bool、数组等)。这可以像将$return 设置为“1”一样简单,这将返回int 而不是对象。检查$return 以了解您的错误来源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多