在PHP中创建配置文件 [英] Creating a config file in PHP

查看:94
本文介绍了在PHP中创建配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的PHP项目创建一个配置文件,但是我不确定执行此操作的最佳方法是什么.

I want to create a config file for my PHP project, but I'm not sure what the best way to do this is.

到目前为止,我有3个主意.

I have 3 ideas so far.

1-使用变量

$config['hostname'] = "localhost";
$config['dbuser'] = "dbuser";
$config['dbpassword'] = "dbpassword";
$config['dbname'] = "dbname";
$config['sitetitle'] = "sitetitle";

2-使用常量

define('DB_NAME', 'test');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
define('TITLE', 'sitetitle');

3-使用数据库

我将在类中使用配置,所以我不确定哪种方法是最佳方法,或者是否有更好的方法.

I will be using the config in classes so I'm not sure which way would be the best or if there is a better way.

推荐答案

一种简单而优雅的方法是创建一个仅返回数组的config.php文件(或您所谓的文件):

One simple but elegant way is to create a config.php file (or whatever you call it) that just returns an array:

<?php

return array(
    'host' => 'localhost',
    'username' => 'root',
);

然后:

$configs = include('config.php');

这篇关于在PHP中创建配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆