简体   繁体   English

PHP对象中的访问数组

[英]Access array in PHP object

I have the following PHP object but I'm struggling to get the array item out of the object. 我有以下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 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? 获取阵列的最佳方法是什么?

If you can edit the class, either change the property you care for to public or write a getter for it: 如果您可以编辑该类,则可以将您需要的属性更改为公共属性,或者为其编写一个getter方法:

function getItems() {
    return $this->items ;
}

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: 否则,如果您不能编辑类本身,则可以扩展它,因为所需的属性受到保护,这意味着子类可以访问它们:

class YourClass extends ThatClass {

    public function getItems {
        //parent $items really
        return $this->items ;
    }

}

Then you'll need to create an instance of YourClass instead of ThatClass and get the items array from it. 然后,您需要创建YourClass的实例而不是ThatClass并从中获取items数组。

Similarly for any other protected properties you want. 对于您想要的任何其他受保护的属性类似。

The feed_nid property of your object is protected, so it cannot be accessed from outside the object. 您对象的feed_nid属性是受保护的,因此无法从对象外部对其进行访问。

Inside the object class, you should write a function like this: 在对象类内部,应编写如下函数:

function getFeedNid()
{
    return $this->feed_nid;
}

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 to public . 最初的意图显然是使该属性在内部保持安全并不受外部修改,因此我将使用此方法,而不是例如将protected $feed_nid声明更改为public

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM