【问题标题】:replace string value in config.php替换 config.php 中的字符串值
【发布时间】:2017-05-10 05:04:45
【问题描述】:

我对 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"          => ""
        ]
    ];

?>

现在我想通过一个函数更改一些设置。我该怎么做?

【问题讨论】:

    标签: php settings backend configuration-files


    【解决方案1】:
    public function replace ($keyset, $value){
    $key_parse = explode(".",keyset);
    $this->data[$key_parse[0]][$key_parse[1]] = $value;
    return $this->data;
    }
    

    在你的 config_system.php 中使用这个函数

    【讨论】:

    • 谢谢您的回答。但该功能显示错误。 explode() 至少需要 2 个参数
    • 抱歉错字了..它
    猜你喜欢
    • 2018-04-05
    • 2015-11-28
    • 2011-10-14
    • 1970-01-01
    • 2023-01-05
    • 2021-09-23
    • 2015-03-27
    相关资源
    最近更新 更多