PHP 访问对象的属性

发布于 2024-09-07 14:48:46 字数 4517 浏览 1 评论 0原文

我有一个通过 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);

I have an object that looks like this when outputted via a 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] => 
                                                        )   


 )

I am trying to access the image file name (see the starred entry), I have tried this currently to no avail,

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

合久必婚 2024-09-14 14:48:46
$this->recipeCategories->_related['_related']['image']

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

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

_lated 是一个数组,您无法像对象一样访问它。只需仔细遵循您在 print_r 输出中看到的内容即可。如果它显示Object,则需要使用->访问子级,如果它显示Array,则使用[]

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

or (hard to tell what's what exactly in what you posted):

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

_related is an array, you can't access it like an object. Just carefully follow what you see in your print_r output. If it says Object, you need to access children with ->, if it says Array, use [].

謸气贵蔟 2024-09-14 14:48:46

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

Try

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

It's a wild mixture of arrays and objects. To get the objects property of the image object, use

Try

 print_r($this->recipeCategories[178]->related["_related"]["image"]->objects);
神爱温柔 2024-09-14 14:48:46

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

Try reorganized your code to use getters and setters, your internal structure is a mess and it will only get messier with time.
http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

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