加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 从变量中的字符串中获取对象数据

发布时间:2020-12-13 17:52:18 所属栏目:PHP教程 来源:网络整理
导读:当对象名称(类别)直接放置时,它返回正确的值,如: $primary = $this-dbmapper-category['primary'] ; // ( Correct Output ) 但是,当将对象名称放在名为$dataname的变量中时,它返回空白,如: $dataname = 'category'; $primary = $this-dbmapper-$dataname['
当对象名称(类别)直接放置时,它返回正确的值,如:
$primary = $this->dbmapper->category['primary'] ;  // ( Correct Output )

但是,当将对象名称放在名为$dataname的变量中时,它返回空白,如:

$dataname = 'category';
 $primary = $this->dbmapper->$dataname['primary'] ;   ( Blank Output )

我的构造函数变量是

$this->dbmapper = $this->mapper();

我的功能是:

function mapper($module='')
    {
        $mapper =  array();
        $mapper['category']['table'] = 'allcategory';
        $mapper['category']['primary'] = 'categoryID';
        $mapper['page']['table'] = 'allpages';
        $mapper['page']['primary'] = 'pageID';
        return (object) $mapper;
    }
要通过变量访问对象属性(特别是如果它是一个数组),请用括号括起来:
...
$dataname = 'category';
$primary = $this->dbmapper->{$dataname}['primary'];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读