2012-02-11 95 views
-2

我想將變量的值放入Zend_Config_Ini的實例中。如何將變量值放入Zend_Config_Ini

舉例來說,如果我事先知道這個名字,我可以做這樣的事情,

$config->name = $value; 

但因爲我在運行時接收的名字,我想做的事:

$config->$var = $value; 

這可能嗎?我試過了,它不起作用。這裏是我的嘗試:

update_settings.php

<?php 
$name = $_POST['key']; 
$new_value = $_POST['new_value']; 

if ($name && $value) 
{ 
    $config = new Zend_Config_Ini('_config/config.ini', null, array('skipExtends' => true, 'allowModifications' => true)); 

    // EDIT 
    $config->$name = $new_value; 

    $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => 'config.ini')); 

    $json['success'] = $writer->write() 
} 
?> 

如果有人已經能夠做到這一點,請告訴我如何。

+1

我會建議反對這樣做這樣的事情。 – markus 2012-09-28 13:16:08

回答

1

當然是可能的(假設我理解你的問題)。我在這裏略微猜測,因爲你只是說「它不工作」,這不是我見過的最好的錯誤信息。

您必須確保您引用的config.ini文件存在,並且您有正確的路徑。像這樣的東西應該爲你工作: -

$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', null, array('skipExtends' => true, 'allowModifications' => true)); 
//The previous line doesn't look quite right in your example. 
$testVariableName = 'test'; 
$testVariableValue = 'testing'; 
$config->$testVariableName = $testVariableValue; 
var_dump($config->test); 

輸出: -

string 'testing' (length=7) 
+0

當網站允許我這樣做時,我會在12個小時內給你獎勵;)謝謝你的回答! – 2012-09-27 07:49:07

+0

沒問題,我很高興你把它整理出來。 – vascowhite 2012-09-27 07:52:11

+0

我也是!經過7個月的錯誤答案! – 2012-09-27 07:53:05

0

我建議你使用isset PHP Manual函數。

if (isset($_POST['id']) && isset($_POST['value'])) { 

$config = new Zend_Config_Ini('../conf/config.ini',null,array('skipExtends' => true,  'allowModifications' => true)); 
    // EDIT 
    $config->$_POST['id'] = $_POST['value']; 
. 
. 
. 

如果id & value未設置你會得到一個:

Notice: Undefined index: ... 
+1

我不認爲這是任何問題的答案,儘管這是很好的建議。 – 2012-02-11 14:41:50

+2

我不認爲你可以有效地執行'$ config - > $ _ POST ['id']'。它應該可能是'$ config - > {$ _ POST ['id']}'。 – 2012-09-26 20:51:35