首页 > 解决方案 > 从对象获取php数组

问题描述

我无法从对象中获取数组:

请在下面查看我的代码:

 print_r($product_ser_id);

打印结果:

WHMCS\Service\Service Object
(
    [table:protected] => tblhosting
    [columnMap:protected] => Array
        (
            [clientId] => userid
            [productId] => packageid
            [serverId] => server
            [registrationDate] => regdate
            [paymentGateway] => paymentmethod
            [status] => domainstatus
            [promotionId] => promoid
            [overrideAutoSuspend] => overideautosuspend
            [overrideSuspendUntilDate] => overidesuspenduntil
            [bandwidthUsage] => bwusage
            [bandwidthLimit] => bwlimit
            [lastUpdateDate] => lastupdate
            [firstPaymentAmount] => firstpaymentamount
            [recurringAmount] => amount
            [recurringFee] => amount
        )
}

在这里我必须从中获得价值[productId]

我已经测试为:$product_ser_id -> attributes:protected;

其显示错误为:

ParseError: syntax error, unexpected ':' in 

我知道有很多例子,所以在你真正理解我的问题之前,请不要重复。

请帮忙

谢谢

标签: phpobject

解决方案


@drawish的回答解决了问题

来源:反射类

代码:

 function accessProtected($obj, $prop) {
  $reflection = new ReflectionClass($obj);
  $property = $reflection->getProperty($prop);
  $property->setAccessible(true);
  return $property->getValue($obj);
}

推荐阅读