访问 PHP 对象中的数组

发布于 2024-09-24 22:08:05 字数 820 浏览 1 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2024-10-01 22:08:05

如果您可以编辑该类,请将您关心的属性更改为 public 或为其编写一个 getter:

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

否则,如果您无法编辑该类本身,您可以扩展它,因为您想要的属性受到保护,这意味着子类可以访问它们:

class YourClass extends ThatClass {

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

}

然后您需要创建 YourClass 而不是 ThatClass 的实例,并从中获取项目数组。

对于您想要的任何其他受保护的属性也是如此。

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.

追风人 2024-10-01 22:08:05

对象的 feed_nid 属性受到保护,因此无法从对象外部访问它。

在对象类内部,您应该编写一个如下所示的函数:

function getFeedNid()
{
    return $this->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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文