How to access the values of a private object in PHP?

-2

I have the following variable:

var_dump($bankAccount);

What prints:

  

object (PayMe \ Sdk \ BankAccount \ BankAccount) # 35 (11) {   ["id": "PayMe \ Sdk \ BankAccount \ BankAccount": private] = >
  int (17929453)
  ["bankCode": "PayMe \ Sdk \ BankAccount \ BankAccount": private] = >
  string (3) "104"
  ["agency": "Pay Me \ Sdk \ BankAccount \ BankAccount": private] = >
  string (4) "0000"
  ["DV Agent": "PayMe \ Sdk \ BankAccount \ BankAccount": private] = >
  NULL ["account": "PayMe \ Sdk \ BankAccount \ BankAccount": private] = >
  string (5) "12345"
  ["accountDv": "PayMe \ Sdk \ BankAccount \ BankAccount": private] = >
  string (1) "5"
  ["documentNumber": "PayMe \ Sdk \ BankAccount \ BankAccount": private] = >   string (11) "04900000000"
  ["documentType": "PayMe \ Sdk \ BankAccount \ BankAccount": private] = >
  string (3) "cpf"
  ["legalName": "PayMe \ Sdk \ BankAccount \ BankAccount": private] = >
  string (12) "Maykel Esser"
  ["dateCreated": "PayMe \ Sdk \ BankAccount \ BankAccount": private] = >
  object (DateTime) # 37 (3) {       ["date"] = >       string (26) "2019-01-02 20: 03: 14.031000"       ["timezone_type"] = >       int (2)       ["timezone"] = >       string (1) "Z"} ["type": "PayMe \ Sdk \ BankAccount \ BankAccount": private] = >
  string (14) "account_current"}

That is, the return of the $ bankAccount var_dump is an object. I need the ID that is inside this object, and save it in a variable for later use.

I tried to pull it this way:

$idBanco = $bankAccount->id;

But I got the error:

  

Fatal error : Uncaught Error: Can not access private property PayMe \ Sdk \ BankAccount \ BankAccount :: $ id

How to proceed?

    
asked by anonymous 02.01.2019 / 21:07

1 answer

0

If the object's property is private, you can not directly access it, add a method to the class that returns that id:

class Exemplo {
     // ...

     public function getId() {
          return $this->id;
     }
 }

If this is not possible, you can create another class that will extend the first one and use it instead

class MeuExemplo extends Exemplo {
     public function getId() {
          return $this->id;
      }
 }

However, the id property must be of type protected

    
02.01.2019 / 23:05