【问题标题】:PHP accessing a property of an objectPHP 访问对象的属性
【发布时间】:2011-03-09 13:02:05
【问题描述】:

我有一个通过print_r 输出时看起来像这样的对象

   Array
     (
         [178] => My_Model_Category Object
        (
            [autoGenerateURLNameIfNotSupplied] => 1
            [id] => 178
            [name] => Just for Kids
            [date_created] => 2010-04-06 16:08:40
            [last_updated] => 2010-06-29 10:29:50
            [user_id_updated] => 0
            [_table] => 
            [_aliases] => Array
                (
                    [id] => 178
                    [name] => Just for Kids
                    [date_created] => 2010-04-06 16:08:40
                    [date_updated] => 2010-06-29 10:29:50
                    [user_id_updated] => 0
                    [parent_id] => 
                    [url_name] => just-for-kids
                    [description] => 
                    [image_id] => 
                    [image_id_teaser] => 109
                    [cat_usage] => recipes
                    [rank] => 
                    [note] => 
                )

            [_nonDBAliases] => Array
                (
                )

            [_default] => Array
                (
                )

            [_related] => Array
                (
                    [_related] => Array
                        (
                            [image] => stdClass Object
                                (
                                    [key] => image
                                    [group] => _related
                                    [foreignKey] => image_id_teaser
                                    [indexName] => id
                                    [tableName] => jm_asset
                                    [objectName] => Common_Model_Asset
                                    [userFieldlyColName] => name
                                    [criteria] => id='{%image_id_teaser%}'
                                    [sqlPostfix] => ORDER BY rank ASC
                                    [populateOnLoad] => 1
                                    [objects] => Array
                                        (
                                            [109] => Common_Model_Asset Object
                                                (
                                                    [id] => 109
                                                    [name] => 
                                                    [date_created] => 2010-03-29 15:07:25
                                                    [last_updated] => 2010-03-29 15:07:25
                                                    [user_id_updated] => 0
                                                    [_table] => 
                                                    [_aliases] => Array
                                                        (
                                                            [id] => 109
                                                            [name] => 
                                                            [date_created] => 2010-03-29 15:07:25
                                                            [date_updated] => 2010-03-29 15:07:25
                                                            [user_id_updated] => 0
                                                            [asset_usage] => 
                                                            [url] => /x/img/dyn/recipe/my-recipe-26-image.jpg
                                                            [type] => recipe_image
                                                            **[filename] => my-recipe-26-image.jpg**
                                                            [fileext] => .jpg
                                                            [filesize] => 
                                                            [width] => 250
                                                            [height] => 250
                                                            [scale] => 
                                                            [rank] => 
                                                            [note] => 
                                                        )   


 )

我正在尝试访问图像文件名(请参阅带星号的条目),我目前已尝试过,但无济于事,

print_r($this->recipeCategories->_related->_related->images);

【问题讨论】:

  • 它看起来像来自 ORM 的对象。相应的文档应该揭示如何正确访问属性。我认为您不应该直接访问它们。

标签: php arrays object multidimensional-array


【解决方案1】:
$this->recipeCategories->_related['_related']['image']

或者(很难说出你发布的内容到底是什么):

$this->recipeCategories[178]->_related['_related']['image']

_related 是一个数组,你不能像对象一样访问它。请仔细按照您在print_r 输出中看到的内容进行操作。如果显示Object,则需要使用-> 访问子级,如果显示Array,则使用[]

【讨论】:

    【解决方案2】:

    这是数组和对象的狂野混合。要获取image 对象的objects 属性,请使用

    试试

     print_r($this->recipeCategories[178]->related["_related"]["image"]->objects);
    

    【讨论】:

      【解决方案3】:

      尝试重新组织你的代码以使用 getter 和 setter,你的内部结构是一团糟,它只会随着时间的推移变得更加混乱。 http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-23
        • 2014-10-10
        • 2012-07-30
        • 1970-01-01
        • 2013-07-19
        • 2018-11-12
        • 2018-06-12
        • 2021-03-05
        相关资源
        最近更新 更多