从存储在变量中的字符串中获取对象数据

时间:2016-12-18 17:54:38

标签: php class object

当对象名称(类别)直接放入时,它返回正确的值,如:

 $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;
    }

1 个答案:

答案 0 :(得分:5)

要通过变量访问对象属性(特别是如果它是一个数组),用括号括起来:

...
$dataname = 'category';
$primary = $this->dbmapper->{$dataname}['primary'];