我执行了一个函数,它返回一个对象。对象的var_dump可以在以下位置看到:
视图源:http://quemfazsite.com.br/temp/boleto/teste.php
我想访问id
内的属性_attributes
。我该怎么做?
我试过
$returned_value -> _attributes -> id
但它显示为NULL。我做错了什么?
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) "teste@teste.com"
["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: teste@teste.com"
["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)
}
}
答案 0 :(得分:2)
首先,变量 _attributes 是受保护的,您需要一个getter方法。由于您无法访问Iugu_Invoice类来添加它,因此可以扩展类:
use Namespace\To\Iugu_Invoice;
class MyIugu_Invoice extends Iugu_Invoice{
public function getAttributes(){
return $this->_attributes
}
}
请注意在上面的代码中使用真正的命名空间。如果你不使用命名空间就忘记那一点。然后你需要使用MyIugu_Invoice而不是Iugu_Invoice。 此外,_attributes是一个数组而不是Object,因此您需要执行:
$attributes = $returned_value->getAttributes();
$id = $attributes['id'];
答案 1 :(得分:0)
看起来id在数组中,而不是在class属性中。 试试这个:
$returned_value->_attributes["id"]
如上所述,您无法访问该数组,因为它受到保护,如果您无法在类中添加getter(如您所述),则必须从该类继承,并且添加一个吸气剂。
答案 2 :(得分:0)
试试这个:
echo $returned_value["id"];
或者这个:
$attr = (object) array_shift($returned_value);
echo $attr->id;