我对PHP知之甚少。我的项目中有3个文件。
第一个是system.php,它保存了空洞应用程序逻辑。 这里是它的代码:
<?php
require "config/config_system.php";
$config = new Config;
$config-> load('config.php');
// this is way i want to change setting.
echo $config-> replace("db.host" , "replace value");
?>
第二个是config_system.php,它包含配置逻辑。其代码为:
<?php
class Config {
protected $data;
protected $informaton;
protected $default;
public function load($file) {
$this->data = require $file;
$this->informaton = require $file;
}
public function find($key, $default = null) {
$this->default = $default;
$segments = explode(".", $key);
$data = $this->data;
foreach ($segments as $segment) {
if (isset($data[$segment])) {
$data = $data[$segment];
} else {
$data = $this->default;
break;
}
}
return $data;
}
public function exists($key) {
return $this->find($key) !== $this->default;
}
// this is the function i am trying to make valide
public function replace($value) {
$arrayvalues = explode(".", $value);
$informaton = $this->informaton;
foreach ($arrayvalues as $arrayvalue) {
if (isset($informaton[$arrayvalue])) {
$informaton = $informaton[$arrayvalue];
}
}
return $arrayvalues;
}
}
?>
和第三个是config.php,它保存配置。
<?php
return [
"installation" => [
// this is the value I want to change via a function to true.
"create_db" => "false",
"create_table" => "false"
],
"db" => [
"host" => "localhost",
"user_name" => "root",
"password" => ""
]
];
?>
现在我想通过一个函数更改一些设置。我该怎么办?
答案 0 :(得分:0)
public function replace ($keyset, $value){
$key_parse = explode(".",keyset);
$this->data[$key_parse[0]][$key_parse[1]] = $value;
return $this->data;
}
在config_system.php中使用此功能