Reputation: 704
I executed a function and it returns an object. The var_dump of the object can be seen at:
view-source:http://quemfazsite.com.br/temp/boleto/teste.php
I want to access the property id
inside of _attributes
. How do I do that?
I tried
$returned_value -> _attributes -> id
but it shows NULL. What am I doing wrong?
object(Iugu_Invoice)#7 (2) {
["_attributes":protected]=>
array(51) {
["id"]=>
string(32) "B2EC65C567244A69BBFD4513CC5D460E"
["due_date"]=>
string(10) "02/09/2015"
["currency"]=>
string(3) "BRL"
["discount_cents"]=>
NULL
["email"]=>
string(15) "[email protected]"
["items_total_cents"]=>
int(1000)
["notification_url"]=>
NULL
["return_url"]=>
NULL
["status"]=>
string(7) "pending"
["tax_cents"]=>
NULL
["updated_at"]=>
string(25) "2015-08-28T11:32:09-03:00"
["total_cents"]=>
int(1000)
["total_paid_cents"]=>
int(0)
["paid_at"]=>
NULL
["taxes_paid_cents"]=>
NULL
["paid_cents"]=>
NULL
["cc_emails"]=>
NULL
["financial_return_date"]=>
NULL
["payable_with"]=>
string(9) "bank_slip"
["overpaid_cents"]=>
NULL
["secure_id"]=>
string(41) "b2ec65c5-6724-4a69-bbfd-4513cc5d460e-f4a3"
["secure_url"]=>
string(67) "https://iugu.com/invoices/b2ec65c5-6724-4a69-bbfd-4513cc5d460e-f4a3"
["customer_id"]=>
NULL
["customer_ref"]=>
NULL
["customer_name"]=>
NULL
["user_id"]=>
NULL
["total"]=>
string(8) "R$ 10,00"
["taxes_paid"]=>
string(7) "R$ 0,00"
["total_paid"]=>
string(7) "R$ 0,00"
["total_overpaid"]=>
string(7) "R$ 0,00"
["fines_on_occurrence_day"]=>
NULL
["total_on_occurrence_day"]=>
NULL
["fines_on_occurrence_day_cents"]=>
NULL
["total_on_occurrence_day_cents"]=>
NULL
["advance_fee"]=>
NULL
["paid"]=>
string(7) "R$ 0,00"
["interest"]=>
NULL
["discount"]=>
NULL
["created_at"]=>
string(14) "28/08, 11:32 h"
["refundable"]=>
NULL
["installments"]=>
NULL
["transaction_number"]=>
int(1111)
["payment_method"]=>
NULL
["created_at_iso"]=>
string(25) "2015-08-28T11:32:09-03:00"
["updated_at_iso"]=>
string(25) "2015-08-28T11:32:09-03:00"
["financial_return_dates"]=>
NULL
["bank_slip"]=>
object(stdClass)#3 (3) {
["digitable_line"]=>
string(47) "00000000000000000000000000000000000000000000000"
["barcode_data"]=>
string(44) "00000000000000000000000000000000000000000000"
["barcode"]=>
string(75) "https://iugu.com/invoices/barcode/b2ec65c5-6724-4a69-bbfd-4513cc5d460e-f4a3"
}
["items"]=>
array(1) {
[0]=>
array(3) {
["description"]=>
string(32) "Serviço Digital CÓD. 873923675"
["quantity"]=>
string(1) "1"
["price_cents"]=>
string(4) "1000"
}
}
["variables"]=>
array(1) {
[0]=>
object(stdClass)#5 (3) {
["id"]=>
string(32) "B16E017E3D124AA38C2B8F97E2723C5B"
["variable"]=>
string(31) "payment_data.transaction_number"
["value"]=>
string(4) "1111"
}
}
["custom_variables"]=>
array(0) {
}
["logs"]=>
array(1) {
[0]=>
object(stdClass)#6 (4) {
["id"]=>
string(32) "4FC3CF74C5184DBE9C3F7B2DC421E308"
["description"]=>
string(26) "Email de Lembrete enviado!"
["notes"]=>
string(50) "Lembrete enviado com sucesso para: [email protected]"
["created_at"]=>
string(14) "28/08, 11:32 h"
}
}
}
["_unsavedAttributes":protected]=>
array(4) {
["due_date"]=>
int(1)
["email"]=>
int(1)
["payable_with"]=>
int(1)
["items"]=>
int(1)
}
}
Upvotes: 1
Views: 212
Reputation: 1
try this:
echo $returned_value["id"];
or this:
$attr = (object) array_shift($returned_value);
echo $attr->id;
Upvotes: 0
Reputation: 1168
First of all the variable _attributes is protected, you will need a getter method. As you cannot access the Iugu_Invoice class to add it, you can extend the class:
use Namespace\To\Iugu_Invoice;
class MyIugu_Invoice extends Iugu_Invoice{
public function getAttributes(){
return $this->_attributes
}
}
Please bear in mind to use the real namespace in the code above. If you do not use namespaces just forget that bit. Then you need to use MyIugu_Invoice instead of Iugu_Invoice. Also, _attributes is an array not an Object, so you will need to do:
$attributes = $returned_value->getAttributes();
$id = $attributes['id'];
Upvotes: 2
Reputation: 6016
Looks like the id is in an array, not in a class property. Try this:
$returned_value->_attributes["id"]
And as mentioned you can't access that array because it's protected, if you can't add a getter in the class (as you mentioned) you have to inherit from that class, and add a getter.
Upvotes: 0