我在PHP OOPS代码中遇到问题。
我的代码是:
class Settings
{
private $client_addr = array(
'ClientID' => array('maxlength'=>'10','IsNull'=>'n'),
'ClientAddressType' => array('maxlength'=>'12','IsNull'=>'y'),
'ClientAddressLine1' => array('maxlength'=>'30','IsNull'=>'y'),
'ClientAddressLine2' => array('maxlength'=>'30','IsNull'=>'y'),
'ClientCounty' => array('maxlength'=>'30','IsNull'=>'y'),
'ClientCity' => array('maxlength'=>'30','IsNull'=>'y'),
'ClientState' => array('maxlength'=>'2','IsNull'=>'y'),
'ClientZip' => array('maxlength'=>'9','IsNull'=>'y'),
);
private $client_general = array(
'PayerID' => array('maxlength'=>'64','IsNull'=>'n'),
'ProviderID' => array('maxlength'=>'50','IsNull'=>'n'),
'ClientID' => array('maxlength'=>'10','IsNull'=>'n'),
'ClientFirstName' => array('maxlength'=>'30','IsNull'=>'n'),
'ClientMiddleInitial' => array('maxlength'=>'1','IsNull'=>'y'),
'ClientLastName' => array('maxlength'=>'30','IsNull'=>'n'),
);
function getSelectedArrayData($setlected_arr)
{
$setlected_arr = '$this->'."$setlected_arr";
print_r($setlected_arr); //it prints a string '$this->client_general'
print_r($this->client_general);//it prints $client_general array data
}
}
$settings = new Settings();
$settings->getSelectedArrayData('client_general');
我的问题是:
当我打印print_r($this->client_general);
时,它可以打印$client_general
数组。
Array
(
[PayerID] => Array
(
[maxlength] => 64
[IsNull] => n
)
[ProviderID] => Array
(
[maxlength] => 50
[IsNull] => n
)
[ClientID] => Array
(
[maxlength] => 10
[IsNull] => n
)
[ClientFirstName] => Array
(
[maxlength] => 30
[IsNull] => n
)
[ClientMiddleInitial] => Array
(
[maxlength] => 1
[IsNull] => y
)
[ClientLastName] => Array
(
[maxlength] => 30
[IsNull] => n
)
)
当我打印print_r($setlected_arr);
时
$this->client_general
我认为它也应该指向$client_general
数组。
我该怎么做?
答案 0 :(得分:3)
print_r($this->$setlected_arr);
答案 1 :(得分:2)
尝试一下,它为您服务。
class Settings {
private $client_addr = array(
'ClientID' => array('maxlength' => '10', 'IsNull' => 'n'),
'ClientAddressType' => array('maxlength' => '12', 'IsNull' => 'y'),
'ClientAddressLine1' => array('maxlength' => '30', 'IsNull' => 'y'),
'ClientAddressLine2' => array('maxlength' => '30', 'IsNull' => 'y'),
'ClientCounty' => array('maxlength' => '30', 'IsNull' => 'y'),
'ClientCity' => array('maxlength' => '30', 'IsNull' => 'y'),
'ClientState' => array('maxlength' => '2', 'IsNull' => 'y'),
'ClientZip' => array('maxlength' => '9', 'IsNull' => 'y'),
);
private $client_general = array(
'PayerID' => array('maxlength' => '64', 'IsNull' => 'n'),
'ProviderID' => array('maxlength' => '50', 'IsNull' => 'n'),
'ClientID' => array('maxlength' => '10', 'IsNull' => 'n'),
'ClientFirstName' => array('maxlength' => '30', 'IsNull' => 'n'),
'ClientMiddleInitial' => array('maxlength' => '1', 'IsNull' => 'y'),
'ClientLastName' => array('maxlength' => '30', 'IsNull' => 'n'),
);
function getSelectedArrayData($setlected_arr) {
$setlected_arr = ${'this'}->${'setlected_arr'};
print_r($setlected_arr); //it prints a string '$this->client_general'
echo '<br/>';
echo '<br/>';
print_r($this->client_general); //it prints $client_general array data
}
}
$settings = new Settings();
$settings->getSelectedArrayData('client_general');
您要通过字符串创建动态变量的示例在这里:
${'a' . 'b'} = 'hello there';
echo $ab; // hello there