0

我想知道是否可以在运行任何脚本之前向 php 添加常量,因此在启动时。如果这是可能的,它也可以用类等来完成吗?

我正在考虑为php创建插件的方向,但也许有一种更简单的方法。

我并不是说在每个脚本中都包含一个文件。

提前致谢

4

3 回答 3

3

据我所知,不是常数,但这没关系:

.htaccess

SetEnv MYVAR "hello"

一些文件.php

echo $_SERVER['MYVAR']; 

有关更多信息,请参阅SetEnv 上的 Apache 文档

于 2012-05-22T23:30:33.397 回答
1

To directly answer the question, there are two approaches:

  1. Use auto_prepend_file to auto include a PHP file that has define calls.

  2. Configure your web server to set server variables.

I think the second is a better approach. However, I don't see how either of them are very useful in the context of a plugin. Usually a class autoloader of some sort is the way to go there, or to require a single include file.

于 2012-05-22T23:32:54.157 回答
0

If I understand your question correctly, what I do is to include a file before all else on my index.php. That same file contains tons of constants, control verifications, initialization for the DB object, etc...

e.g.,

INSIDE index.php

<?php

$moduleRoot = dirname(__FILE__);
require_once($moduleRoot."/components/inc/inc.php");

// continue to render the web page and perform as usual

?>

INSIDE THE inc.php

// When in development, all errors should be presented
// Comment this two lines when in production
error_reporting(E_ALL);
ini_set('display_errors', '1');

// Website id for this project
// the website must be present in the table site in order to get
// the configurations and records that belong to this website
define("CONF_SITE_ID",1);

// Domain path where the project is located
// Should be like the access used on the browser
$serverDomain = $_SERVER["HTTP_HOST"];
$serverAccess = (!empty($_SERVER['HTTPS'])) ? ('https://') : ('http://');
$serverRoot = dirname(__FILE__);
define("CONF_DOMAIN", $serverAccess.$serverDomain);

// etc ...

EDITED

Since you have multiple "startup" files and you need all of them to call inc.php, the best choise seems to be .user.ini as of PHP 5.3.0, you can read about it here!.

And an article on the subject.

于 2012-05-22T23:34:20.200 回答