简体   繁体   中英

Access array in PHP object

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?

If you can edit the class, either change the property you care for to public or write a getter for it:

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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