访问对象的属性,就像对象是数组一样 [英] Accessing object's properties like if the objects were arrays

查看:27
本文介绍了访问对象的属性,就像对象是数组一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您不知道对象的名称将如何书写时,是否可以访问对象的属性?

Is it possible to access object's properties, when you don't know just how their names will be written?

我的问题是,当查询返回 Zend_Db_Table_Rowset_Abstract 对象时,有一些字段的名称类似于name_fr"、name_en"、name_au".我想根据应用程序中使用的当前语言访问它们中的任何一个.为了实现这一点,我这样编写代码:

My problem is that when a query returns Zend_Db_Table_Rowset_Abstract object, there are some fields with names like "name_fr", "name_en", "name_au". I want to access either of them according to the current language used in the application. To achieve this I write the code this way:

$result = $myModel->fetchAll($query)->current();
$row = $result->toArray();
echo 'Your name is '.$row['name_'.$language];

这很烦人.是否可以编写这样的代码,例如:

This is very annoying. Is it possible to write a code like this for example:

$result = $myModel->fetchAll($query)->current();
echo 'Your name is '.$result->name_{$language};

推荐答案

这应该有效:

$result = $myModel->fetchAll($query)->current();
echo 'Your name is '.$result->{'name_'.$language};

这篇关于访问对象的属性,就像对象是数组一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆