当对象名称(类别)直接放入时,它返回正确的值,如:
$primary = $this->dbmapper->category['primary'] ; // ( Correct Output )
Run Code Online (Sandbox Code Playgroud)
但是,当将对象名称放在名为$ dataname的变量中时,它返回空白,如:
$dataname = 'category';
$primary = $this->dbmapper->$dataname['primary'] ; ( Blank Output )
Run Code Online (Sandbox Code Playgroud)
我的构造函数变量是:
$this->dbmapper = $this->mapper();
Run Code Online (Sandbox Code Playgroud)
我的功能是:
function mapper($module='')
{
$mapper = array();
$mapper['category']['table'] = 'allcategory';
$mapper['category']['primary'] = 'categoryID';
$mapper['page']['table'] = 'allpages';
$mapper['page']['primary'] = 'pageID';
return (object) $mapper;
}
Run Code Online (Sandbox Code Playgroud)
要通过变量访问对象属性(特别是如果它是一个数组),请用括号括起来:
...
$dataname = 'category';
$primary = $this->dbmapper->{$dataname}['primary'];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63 次 |
| 最近记录: |