【问题标题】:Access an Attribute of an Object with PHP使用 PHP 访问对象的属性
【发布时间】:2013-08-14 20:47:00
【问题描述】:

我正在尝试使用 PHP 访问对象的一部分。我可以访问某些部分,但是,当我尝试访问包含信用卡号最后 4 位数字的部分时,它只会打印出 NULL。这是我的代码:

$customer = Braintree_Customer::find('51658462');
echo var_dump($customer);

这将打印出对象。这是打印出来的一部分:

object(Braintree_Customer)#1 (1) {
  ["_attributes"]=>
  array(14) {
    ["id"]=>
    string(8) "51658462"
    ["merchantId"]=>
    string(16) "tyn83x4j454dnvmt"
    ["firstName"]=>
    string(4) "Demo"
    ["lastName"]=>
    string(7) "Account"
    ["company"]=>
    NULL
    ["phone"]=>
    string(12) "555-555-5555"
    ["fax"]=>
    NULL
    ["website"]=>
    NULL
    ["createdAt"]=>
    object(DateTime)#8 (3) {
      ["date"]=>
      string(19) "2013-08-14 15:19:26"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(3) "UTC"
    }
    ["updatedAt"]=>
    object(DateTime)#9 (3) {
      ["date"]=>
      string(19) "2013-08-14 15:19:27"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(3) "UTC"
    }
    ["customFields"]=>
    string(3) "
  "
    ["creditCards"]=>
    array(1) {
      [0]=>
      object(Braintree_CreditCard)#2 (1) {
        ["_attributes"]=>
        array(29) {
          ["bin"]=>
          string(6) "371275"
          ["expirationMonth"]=>
          string(2) "01"
          ["expirationYear"]=>
          string(4) "2016"
          ["last4"]=>
          string(4) "1234"

我正在尝试访问last4 属性。我尝试了几种不同的方法,但都没有奏效。我认为echo var_dump($customer->creditCards->last4); 会给我价值,但事实并非如此。我错过了什么?

【问题讨论】:

    标签: php arrays object braintree


    【解决方案1】:
    var_dump($customer->creditCards[0]->last4);
    

    creditCards 是一个包含一个项目的数组 - 仅供参考,您不需要 echo var_dump 的结果。

    【讨论】:

      猜你喜欢
      • 2011-03-09
      • 2011-06-10
      • 2013-06-11
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2014-10-10
      • 2012-07-30
      • 2019-12-30
      相关资源
      最近更新 更多