如何明智地使用配置文件和自动加载器
我创建了一个如下所示的配置文件:
$conf['db_hostname'] = "localhost";
$conf['db_username'] = "username";
$conf['db_password'] = "password";
$conf['db_name'] = "sample";
$conf['db_type'] = "mysql";
$conf['db_prefix'] = "exp";
并将其保存为 config.php。
同样,自动加载器看起来像
class autoloader {
public static $loader;
public static function init()
{
if(self::$loader == NULL) {
self::$loader = new self();
}
return self::$loader;
}
public function __construct()
{
spl_autoload_register(array(this, 'library'));
spl_autoload_register(array(this, 'controller'));
spl_autoload_register(array(this, 'helper'));
spl_autoload_register(array($this, 'model'));
}
public function library($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/lib');
spl_autoload_extensions('.php');
spl_autoload($class);
}
public function controller($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/controller');
spl_autoload_extensions('.php');
spl_autoload($class);
}
public function helper($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/helper');
spl_autoload_extensions('.php');
spl_autoload($class);
}
public function model($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/model');
spl_autoload_extensions('.php');
spl_autoload($class);
}
}
- 我应该将这两个文件放置在哪里?在根文件夹中?
- 这些
$config
如何在整个应用程序中可用? - 我该如何在index.php 中使用它们?我应该为
$config
数组创建一个类吗? - 我如何拒绝直接访问 config.php 文件?
I have created a config file that looks like:
$conf['db_hostname'] = "localhost";
$conf['db_username'] = "username";
$conf['db_password'] = "password";
$conf['db_name'] = "sample";
$conf['db_type'] = "mysql";
$conf['db_prefix'] = "exp";
and saved it as config.php.
Similarly the autoloader looks like
class autoloader {
public static $loader;
public static function init()
{
if(self::$loader == NULL) {
self::$loader = new self();
}
return self::$loader;
}
public function __construct()
{
spl_autoload_register(array(this, 'library'));
spl_autoload_register(array(this, 'controller'));
spl_autoload_register(array(this, 'helper'));
spl_autoload_register(array($this, 'model'));
}
public function library($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/lib');
spl_autoload_extensions('.php');
spl_autoload($class);
}
public function controller($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/controller');
spl_autoload_extensions('.php');
spl_autoload($class);
}
public function helper($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/helper');
spl_autoload_extensions('.php');
spl_autoload($class);
}
public function model($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/model');
spl_autoload_extensions('.php');
spl_autoload($class);
}
}
- Where should I place both these files? In Root folder?
- How these
$config
s will be available across the application? - How whould I use them in index.php? Should I create for
$config
array also a class? - How may I deny direct access to config.php file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须包含您的配置文件。我建议使用 require_once。将此代码添加到需要配置变量的任何文件中。最好的方法是使用控制器文件,通常是index.php。这样您只需将 require_once 添加到单个文件中。
不用担心人们查看您的数据库密码,所有 php 变量都是纯粹的服务器端,除非显式回显,否则客户端无法查看任何 php 代码或变量。
You must include your config file. I would recommend using require_once. Add this code to any files that will need the config variables. The best way to do this is to use a controller file, usually an index.php. That way you only need to add the require_once to a single file.
Don't worry about people viewing your database password, all php variables are purely server-side, and no php code or variables can be viewed by a client unless explicitly echoed out.