【问题标题】:Get php array from object从对象获取php数组
【发布时间】:2019-06-05 02:01:17
【问题描述】:

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

请看下面我的代码:

 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 

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

请帮忙

谢谢

【问题讨论】:

  • 你没有吸气剂吗?
  • 如果不扩展 WHMCS\Service\Service 或更改 WHMCS\Service\Service 类,您将无法获取该数组,因为它是受保护的。您可以创建一个适配器并扩展服务,然后创建一个返回它的方法。
  • @CésarFerreira 请帮忙,大多数人都在说重复而没有理解受保护

标签: php object


【解决方案1】:

@drewish的回答解决的问题

来源:ReflectionClass

代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多