访问 PHP 对象中的数组
我有以下 PHP 对象,但我正在努力从该对象中获取数组项。
exampleBatch Object (
[file_path:protected] =>
[title:protected] =>
[description:protected] =>
[link:protected] =>
[items:protected] => Array ( )
[raw:protected] => data/example
[feed_nid:protected] =>
Array (
[0] => Array ( [path] => data/example/example/ [filename] => file.csv )
[1] => Array ( [path] => data/example/example/ [filename] => file.csv )
[2] => Array ( [path] => dexampleata/example// [filename] => file.csv ) )
[current_item:protected] =>
[created] => 0
[updated] => 0
[total:protected] => Array ( )
[progress:protected] => Array ( [fetching] => 1 [parsing] => 1 [processing] => 1 ) )
我需要访问包含三个键的数组及其数据以进行某些后处理。
获取数组的最佳方法是什么?
I have the following PHP object but I'm struggling to get the array item out of the object.
exampleBatch Object (
[file_path:protected] =>
[title:protected] =>
[description:protected] =>
[link:protected] =>
[items:protected] => Array ( )
[raw:protected] => data/example
[feed_nid:protected] =>
Array (
[0] => Array ( [path] => data/example/example/ [filename] => file.csv )
[1] => Array ( [path] => data/example/example/ [filename] => file.csv )
[2] => Array ( [path] => dexampleata/example// [filename] => file.csv ) )
[current_item:protected] =>
[created] => 0
[updated] => 0
[total:protected] => Array ( )
[progress:protected] => Array ( [fetching] => 1 [parsing] => 1 [processing] => 1 ) )
I need to access array containing the three keys and it's data for some post processing.
Whats the best way to go about grabbing the array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以编辑该类,请将您关心的属性更改为 public 或为其编写一个 getter:
否则,如果您无法编辑该类本身,您可以扩展它,因为您想要的属性受到保护,这意味着子类可以访问它们:
然后您需要创建 YourClass 而不是 ThatClass 的实例,并从中获取项目数组。
对于您想要的任何其他受保护的属性也是如此。
If you can edit the class, either change the property you care for to public or write a getter for it:
Otherwise if you can't edit the class itself, you can extend it since the properties you want are protected which means a child class can access them:
Then you'll need to create an instance of YourClass instead of ThatClass and get the items array from it.
Similarly for any other protected properties you want.
对象的
feed_nid
属性受到保护,因此无法从对象外部访问它。在对象类内部,您应该编写一个如下所示的函数:
最初的意图显然是使该属性保持在内部并免受外部修改,因此我将使用此方法,而不是更改
protected $feed_nid< /code> 声明为
public
。The
feed_nid
property of your object is protected, so it cannot be accessed from outside the object.Inside the object class, you should write a function like this:
The original intent was obviously to keep that property internal and safe from external modification, so I would use this method instead of, for example, changing the
protected $feed_nid
declaration topublic
.