stdClass对象,方法存在吗?

时间:2014-04-01 11:06:54

标签: php object stdclass

我正在使用SOAP与支付平台进行通信,我几乎就在那里,唯一的问题是我似乎无法将服务器的答案转换为行动

我需要检查一下付款情况'方法存在

我已尝试使用 method_exists ,如下所示

if(property_exists($response, 'payment')){
    echo 'PAYMENT EXISTS';
} else {
    echo 'PAYMENT doesnt exist';
}

但它总是回来付款并不存在,我做错了检查吗?谢谢!

这里是$ response对象的print_r

stdClass Object
(
[statusSuccess] => stdClass Object
    (
        [success] => stdClass Object
            (
                [_] => Operation successful.
                [code] => SUCCESS
            )

        [report] => stdClass Object
            (
                [approximateTotals] => stdClass Object
                    (
                        [totalRegistered] => 1500
                        [totalShopperPending] => 0
                        [totalAcquirerPending] => 0
                        [totalAcquirerApproved] => 1500
                        [totalCaptured] => 0
                        [totalRefunded] => 0
                        [totalChargedback] => 0
                        [exchangedTo] => EUR
                        [exchangeRateDate] => 2014-04-01 13:02:30
                    )

                [payment] => stdClass Object
                    (
                        [id] => 4906949180
                        [paymentMethod] => MASTERCARD
                        [authorization] => stdClass Object
                            (
                                [status] => AUTHORIZED
                                [amount] => stdClass Object
                                    (
                                        [_] => 1500
                                        [currency] => EUR
                                    )

                                [confidenceLevel] => ACQUIRER_APPROVED
                                [capture] => stdClass Object
                                    (
                                        [status] => NEW
                                        [amount] => stdClass Object
                                            (
                                                [_] => 1500
                                                [currency] => EUR
                                            )

                                    )

                            )

                    )

            )

    )

)

1 个答案:

答案 0 :(得分:1)

试试这个:

if (
   property_exists($response, 'statusSuccess')
   && property_exists($response->statusSuccess, 'report')
   && property_exists($response->statusSuccess->report, 'payment')
) {
    echo 'payment method exists';
}