簡體   English   中英

PHP對象中的訪問數組

[英]Access array in PHP 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 ) )

我需要訪問包含三個鍵的數組,它是一些后期處理的數據。

獲取陣列的最佳方法是什么?

如果您可以編輯該類,則可以將您需要的屬性更改為公共屬性,或者為其編寫一個getter方法:

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

否則,如果您不能編輯類本身,則可以擴展它,因為所需的屬性受到保護,這意味着子類可以訪問它們:

class YourClass extends ThatClass {

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

}

然后,您需要創建YourClass的實例而不是ThatClass並從中獲取items數組。

對於您想要的任何其他受保護的屬性類似。

您對象的feed_nid屬性是受保護的,因此無法從對象外部對其進行訪問。

在對象類內部,應編寫如下函數:

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

最初的意圖顯然是使該屬性在內部保持安全並不受外部修改,因此我將使用此方法,而不是例如將protected $feed_nid聲明更改為public

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM