【发布时间】:2022-01-14 15:48:18
【问题描述】:
我有这个对象定义
return (object) [
// the database information
'db_info' => [
'type' => 'mysql',
'server' => 'localhost',
'database' => 'myshop',
'username' => 'root',
'password' => 'password',
'tableprefix' => 'password',
'charset' => 'password'
],
// the database tables
'tables_info' => [
'tblcountries' => db_info.database . '.'. db_info.tableprefix . 'countries',
'tblsettings' => db_info.database . '.'. db_info.tableprefix . 'settings'
]
]
我正在尝试使用在同一个对象中定义的一些数组
在 tables_info 数组中
'tblcountries' => db_info.database . '.'. db_info.tableprefix . 'countries`
有可能吗,如果可以的话怎么办..
【问题讨论】:
-
不,直到您到达该对象的末尾,该对象才存在,因此没有什么可引用的,即使您使用
->正确引用了它 -
访问该对象数组的可能方法是在上面定义一个变量,如 $db_info 的值,然后将其转换为对象,因此您可以在 tables_info 上使用它,如 $db_info->database 等..
-
请注意,
(object)演员表不是递归的。db_info属性是关联数组,而不是对象。