使用包含文件的最佳方法,该文件只包含PHP中的数组

时间:2021-03-05 09:59:04

Kohana's config files look like this.. here is an example of a database config file (simplified)

Kohana的配置文件看起来像这样..这里是一个数据库配置文件的示例(简化)

return array(
    'dbhost' => 'localhost',
    'user'   => 'Tom_Jones'
);

I've also got a CMS which wants the connection details. Whilst the CMS uses a different user (with more rights), I'd like to know the best way to include this file and get the data out of it (so as to not repeat myself for hostname and dbname).

我还有一个想要连接细节的CMS。虽然CMS使用不同的用户(具有更多权限),但我想知道包含此文件并从中获取数据的最佳方法(以便不重复自己的主机名和dbname)。

I haven't thought up of any elegant solutions yet and have not yet dug around Kohana to see how it does it. It's late Friday here so it's probably really obvious to everyone except me.

我还没有想到任何优雅的解决方案,还没有挖掘Kohana,看看它是如何做到的。这是星期五晚上,所以除了我以外,每个人都可能很明显。

Update

My apologies, I forgot to include that this is using Kohana 3!

我道歉,我忘了包含这是使用Kohana 3!

4 个解决方案

#1


1  

In Kohana v3, in the Kohana_Config_Reader class, method load():

在Kohana v3中,在Kohana_Config_Reader类中,方法load():

$config = Arr::merge($config, require $file);

require $file is used to load the array in the configuration file.

require $ file用于在配置文件中加载数组。

#2


1  

I downloaded Kohana and could not files that look like your example, but if you are using the current version you could repurpose the config files like this:

我下载了Kohana并且不能看起来像你的例子,但是如果你使用当前版本,你可以重新调整配置文件,如下所示:

<?php
  // Your script
  define('SYSPATH', 'true'); // So Kohana doesn't kill our script
  $config = array();
  include('path/to/system/config/database.php');

  echo $config['default']['connection']['user']; // Echos database user
?>

#3


1  

http://docs.php.net/function.include says:

http://docs.php.net/function.include说:

Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function.

Let's take your example code

我们来看看你的示例代码

<?php // test2.php
return array(
  'dbhost' => 'localhost',
  'user'   => 'Tom_Jones'
);

and a script that includes test2.php

以及包含test2.php的脚本

<?php
$cfg = include 'test2.php';
if ( !is_array($cfg) ) {
    // ... add useful error handling here ...
}
// you might want to test the structure of $cfg
// before accessing specific elements
echo $cfg['dbhost'];

prints localhost.

打印localhost。

#4


0  

The documentation contains some basic info on how you access those config files. So if you have the following in a file called db.php in application/config:

该文档包含有关如何访问这些配置文件的一些基本信息。因此,如果在application / config中名为db.php的文件中有以下内容:

<?php defined('SYSPATH') or die('No direct script access.');

return array(
    'host' => 'localhost',
    'user'   => 'Tom_Jones'
);

You'd access them like this:

你可以像这样访问它们:

$options = Kohana::config('db');
echo $options['user'];
echo $options['host'];

Or like this:

或者像这样:

echo Kohana::config('db.user');
echo Kohana::config('db.host');

Or like this:

或者像这样:

echo Kohana::config('db')->user;
echo Kohana::config('db')->host;

#1


1  

In Kohana v3, in the Kohana_Config_Reader class, method load():

在Kohana v3中,在Kohana_Config_Reader类中,方法load():

$config = Arr::merge($config, require $file);

require $file is used to load the array in the configuration file.

require $ file用于在配置文件中加载数组。

#2


1  

I downloaded Kohana and could not files that look like your example, but if you are using the current version you could repurpose the config files like this:

我下载了Kohana并且不能看起来像你的例子,但是如果你使用当前版本,你可以重新调整配置文件,如下所示:

<?php
  // Your script
  define('SYSPATH', 'true'); // So Kohana doesn't kill our script
  $config = array();
  include('path/to/system/config/database.php');

  echo $config['default']['connection']['user']; // Echos database user
?>

#3


1  

http://docs.php.net/function.include says:

http://docs.php.net/function.include说:

Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function.

Let's take your example code

我们来看看你的示例代码

<?php // test2.php
return array(
  'dbhost' => 'localhost',
  'user'   => 'Tom_Jones'
);

and a script that includes test2.php

以及包含test2.php的脚本

<?php
$cfg = include 'test2.php';
if ( !is_array($cfg) ) {
    // ... add useful error handling here ...
}
// you might want to test the structure of $cfg
// before accessing specific elements
echo $cfg['dbhost'];

prints localhost.

打印localhost。

#4


0  

The documentation contains some basic info on how you access those config files. So if you have the following in a file called db.php in application/config:

该文档包含有关如何访问这些配置文件的一些基本信息。因此,如果在application / config中名为db.php的文件中有以下内容:

<?php defined('SYSPATH') or die('No direct script access.');

return array(
    'host' => 'localhost',
    'user'   => 'Tom_Jones'
);

You'd access them like this:

你可以像这样访问它们:

$options = Kohana::config('db');
echo $options['user'];
echo $options['host'];

Or like this:

或者像这样:

echo Kohana::config('db.user');
echo Kohana::config('db.host');

Or like this:

或者像这样:

echo Kohana::config('db')->user;
echo Kohana::config('db')->host;