【发布时间】:2018-03-08 08:44:18
【问题描述】:
我有一个带有受保护属性的大对象,一个属性可以是其他对象的数组。我的目标是将整个对象打印为单个嵌套数组。所以我需要将对象转换为数组。
我已经尝试过:
$result = (array) $object;
但这只会将最高级别的对象转换为数组,并且会用奇怪的问号符号混淆我的受保护属性名称。
我也尝试过类似的方法,但这只是返回一个空数组:
$result=json_decode(json_encode($object), true);
这是我的对象的样子:
object(Handling\Model\SearchBooking\Booking)[133]
protected 'jabooknr' => string '018024709' (length=9)
protected 'jitsbooknr' => string '' (length=9)
protected 'status' => string 'Y' (length=1)
protected 'platform' => int 4
protected 'agentid' => string '' (length=6)
protected 'paymentInfo' => null
protected 'transports' =>
array (size=2)
0 =>
object(Handling\Model\SearchBooking\Transport)[145]
protected 'depdate' =>
object(DateTime)[146]
public 'date' => string '2016-12-06 00:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
protected 'carriercode' => string 'TB' (length=2)
protected 'carriernumber' => string '2067' (length=4)
protected 'brochure' => string '' (length=6)
protected 'pax' =>
array (size=2)
0 =>
object(Handling\Model\SearchBooking\Pax)[147]
protected 'id' => int 1
protected 'title' => string 'MRS' (length=3)
protected 'firstname' => string 'MA' (length=7)
protected 'name' => string 'BEN' (length=5)
protected 'age' => int 58
protected 'luggage' => int 20
protected 'handLuggage' => null
1 =>
object(Handling\Model\SearchBooking\Pax)[148]
protected 'id' => int 2
protected 'title' => string 'MR' (length=2)
protected 'firstname' => string 'P' (length=6)
protected 'name' => string 'FT' (length=4)
protected 'age' => int 60
protected 'luggage' => int 20
protected 'handLuggage' => null
protected 'departureAirport' => string 'BRU' (length=3)
protected 'arrivalAirport' => string 'AGP' (length=3)
1 =>
object(Handling\Model\SearchBooking\Transport)[149]
protected 'depdate' =>
object(DateTime)[150]
public 'date' => string '2016-12-13 00:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
protected 'carriercode' => string 'TB' (length=2)
protected 'carriernumber' => string '2068' (length=4)
protected 'brochure' => string '' (length=6)
protected 'pax' =>
array (size=2)
0 =>
object(Handling\Model\SearchBooking\Pax)[151]
protected 'id' => int 1
protected 'title' => string 'MRS' (length=3)
protected 'firstname' => string 'MANE' (length=7)
protected 'name' => string 'BN' (length=5)
protected 'age' => int 58
protected 'luggage' => int 20
protected 'handLuggage' => null
1 =>
object(Handling\Model\SearchBooking\Pax)[152]
protected 'id' => int 2
protected 'title' => string 'MR' (length=2)
protected 'firstname' => string 'PIRE' (length=6)
protected 'name' => string 'FYT' (length=4)
protected 'age' => int 60
protected 'luggage' => int 20
protected 'handLuggage' => null
protected 'departureAirport' => string 'AGP' (length=3)
protected 'arrivalAirport' => string 'BRU' (length=3)
protected 'extraLuggage' => null
编辑
我的班级中有一个方法,我可以在其中“找到”如下所示的结果:
public function findBooking()
{
//here happens a bunch of logic to get the right result
var_dump($object); exit; // this is the result that is show above
return $object;
}
【问题讨论】:
-
在正常使用情况下,您无法打印受保护的内容,因为它在课堂外不可见。你可以用反射来做,但这不是微不足道的,如果你想看一个例子,你可以在这里查看我的调试类github.com/ArtisticPhoenix/Evo/blob/master/Evo/Debug.php 虽然你必须删除顶部的环境检查才能工作(因为我没有'尚未将其分离到它自己的项目中)
-
基本上它们不在课堂之外的可见范围内,如果您将这些更改为
public进行测试,您会看到情况就是这样。这是保护的重点...... -
@ArtisticPhoenix 我不认为这是一个问题,我只需要一种方法将我的结果转换为单个数组以将其作为 JSON 返回,因为它实际上是一个 API 调用响应
-
不是这样的。唯一的另一种方法是在类中编写一个函数,称为
toJson() -
@ArtisticPhoenix 是的,这是目标,但我不知道如何将嵌套对象转换为数组