Zend_Config_ini 动态添加一个部分和(或)键?
我的系统上的每个客户端都有一个 ini 文件,并添加要动态创建的新部分和密钥。
我需要这样的想法:
当前ini文件:
[section_a]
key_a=1
key_b-2
并且需要更改此(使用php/zend)代码:
[section_a]
key_a =1
key_b = 2
[section_b]
key_a = 1
需要添加一个名为section_b的新部分,其中有一个名为key_a的新键,但我没有找到任何Zend_Ini_Config 的方法类似于“$ini->add('section_b','key_a')”。
奥布斯:
Php "magic" like $ini->$new_prop->$new_prop = "1" , dont work to!
任何帮助!
更新
<?php
class SystemConfigHelper
{
public static $data;
public static function load()
{
if(!defined("ACCOUNT_ID"))
return true;
try
{
$url = explode('.', $_SERVER['HTTP_HOST']);
self::$data = new Zend_Config_Ini(ACCOUNTS_PATH . "/" . ACCOUNT_ID . "/system-config.ini",null,array("allowModifications" => true));
return true;
}
catch(Exception $e)
{
die($e->getMessage());
return false;
}
}
public static function save()
{
try
{
$url = explode('.', $_SERVER['HTTP_HOST']);
$writer = new Zend_Config_Writer_Ini(array('config' => self::$data,
'filename' => ACCOUNTS_PATH . "/" . ACCOUNT_ID . "/system-config.ini"));
$writer->write();
}
catch(Exception $e)
{
die($e->getMessage());
return false;
}
}
public static function getParam($section,$key)
{
return self::$data->$section->$key;
}
public static function sync($data)
{
//self::$data = $data;
//return;
foreach(self::$data as $section => $param)
{
foreach($param as $key => $value)
{
self::$data->$section->$key = $data[$section][$key];
}
}
}
public static function getParamAs($section,$key,$as)
{
return self::$data->$section->$key==1?$as:"";
}
}
?>
I have an ini file for each client on my system, and add a new section and key to be created on the fly.
I need some think like this:
Current ini file:
[section_a]
key_a=1
key_b-2
And need to change this (with a php/zend) code, to that:
[section_a]
key_a =1
key_b = 2
[section_b]
key_a = 1
Need to add a new section named section_b whith a new key named key_a , but i don't find any mthod on Zend_Ini_Config like "$ini->add('section_b','key_a')".
Obs:
Php "magic" like $ini->$new_prop->$new_prop = "1" , dont work to!
Any Help!!
Update
<?php
class SystemConfigHelper
{
public static $data;
public static function load()
{
if(!defined("ACCOUNT_ID"))
return true;
try
{
$url = explode('.', $_SERVER['HTTP_HOST']);
self::$data = new Zend_Config_Ini(ACCOUNTS_PATH . "/" . ACCOUNT_ID . "/system-config.ini",null,array("allowModifications" => true));
return true;
}
catch(Exception $e)
{
die($e->getMessage());
return false;
}
}
public static function save()
{
try
{
$url = explode('.', $_SERVER['HTTP_HOST']);
$writer = new Zend_Config_Writer_Ini(array('config' => self::$data,
'filename' => ACCOUNTS_PATH . "/" . ACCOUNT_ID . "/system-config.ini"));
$writer->write();
}
catch(Exception $e)
{
die($e->getMessage());
return false;
}
}
public static function getParam($section,$key)
{
return self::$data->$section->$key;
}
public static function sync($data)
{
//self::$data = $data;
//return;
foreach(self::$data as $section => $param)
{
foreach($param as $key => $value)
{
self::$data->$section->$key = $data[$section][$key];
}
}
}
public static function getParamAs($section,$key,$as)
{
return self::$data->$section->$key==1?$as:"";
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您阅读过 Zend_Config_Writer 上的文档吗?假设您已经加载了整个配置文件(而不仅仅是一部分),您应该能够执行此操作(直接从文档中提取):
Have you read the docs on Zend_Config_Writer? Assuming you've loaded the entire config file (and not just a section) you should be able to do this (pulled right from the docs):