Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在面向对象的PHP中获取数组中的项?_Php_Oop - Fatal编程技术网

如何在面向对象的PHP中获取数组中的项?

如何在面向对象的PHP中获取数组中的项?,php,oop,Php,Oop,我在视图中有以下可用变量: $recent_posts 它是一个数组,所以我对它执行了一个foreach循环,var转储结果如下: <? foreach($recent_posts as $post): ?> <pre><?= var_dump($post) ?></pre> <? endforeach ?> 如何检索每个帖子的值。例如,我如何获得帖子的大图片?我尝试了这个(不知道我在做什么),但毫不奇怪它没有起作用: publ

我在视图中有以下可用变量:

$recent_posts
它是一个数组,所以我对它执行了一个foreach循环,var转储结果如下:

<? foreach($recent_posts as $post): ?>

<pre><?= var_dump($post) ?></pre>

<? endforeach ?>
如何检索每个帖子的值。例如,我如何获得帖子的
大图片
?我尝试了这个(不知道我在做什么),但毫不奇怪它没有起作用:

public function get_large_image(){
   return $this->large_image;
}

$post['obj']->大图像应该可以


该属性受保护,因此除非您在该类中,否则您可能无法访问。

$post['obj']->large\u image
应该这样做


该属性受保护,因此除非您在类中,否则您可能无法访问。

大图像
受保护
,您无法访问受保护的
成员
上下文)

您有两个选项,为
大图像
添加一个
getter
函数,或者将其设置为
公共

getter
是一个公开
private
protected
成员的函数

$refl = new ReflectionClass( $post );
var_dump( $refl->getMethods() );

large\u image
保护
,您无法访问
受保护
成员
类外(
上下文)

您有两个选项,为
大图像
添加一个
getter
函数,或者将其设置为
公共

getter
是一个公开
private
protected
成员的函数

$refl = new ReflectionClass( $post );
var_dump( $refl->getMethods() );

由于
$post->large_image
是受保护的属性,因此不允许在类(或派生类)之外访问它。我认为可能有一种getter方法,通过它您可以检索值(可能类似于
get\u large\u image()

要确定对象上可用的方法,请查看附带类的源代码,或使用反射:

<?php

class MyTag
    extends Tag
{
    // I would personally prefer camelCase: getLargeImage
    // but this might be more in line with the signature of the original class
    public function get_large_image()
    {
        return $this->large_image;
    }
}
如果没有可用的方法来获取值,我不会建议您通过公开属性来更改该类(我认为它受到保护是有原因的),或者根本不更改该类(如果它不是您自己的)

相反,如果可能的话,我建议您扩展该类并为该值创建一个getter方法:


由于
$post->large\u image
是受保护的属性,因此不允许在类(或派生类)之外访问它。我认为可能有一种getter方法,通过它您可以检索值(可能类似于
get\u large\u image()

要确定对象上可用的方法,请查看附带类的源代码,或使用反射:

<?php

class MyTag
    extends Tag
{
    // I would personally prefer camelCase: getLargeImage
    // but this might be more in line with the signature of the original class
    public function get_large_image()
    {
        return $this->large_image;
    }
}
如果没有可用的方法来获取值,我不会建议您通过公开属性来更改该类(我认为它受到保护是有原因的),或者根本不更改该类(如果它不是您自己的)

相反,如果可能的话,我建议您扩展该类并为该值创建一个getter方法:


他无法访问此成员,因为他不在
上下文中他无法访问此成员,因为他不在
上下文中