我想知道是否有可能在运行任何脚本之前向php添加常量,因此在启动时。如果可以的话,可以用类等来完成吗?
我正在考虑为php创建插件的方向,但也许有一种更简单的方法。
我并不是说在每个脚本中都包含一个文件。
提前致谢
答案 0 :(得分:3)
据我所知,这不是常数,但这没关系:
<强>的.htaccess 强>
SetEnv MYVAR "hello"
<强> somefile.php 强>
echo $_SERVER['MYVAR'];
有关详情,请参阅Apache docs on SetEnv。
答案 1 :(得分:1)
要直接回答这个问题,有两种方法:
使用auto_prepend_file
自动包含define
次来电的PHP文件。
配置您的Web服务器以设置服务器变量。
我认为第二种方法更好。但是,我没有看到它们中的任何一个在插件的上下文中是如何非常有用的。通常,某种类的自动加载器是去那里的方式,或者需要一个包含文件。
答案 2 :(得分:0)
如果我正确理解了您的问题,我所做的就是在我include
上的所有其他内容之前index.php
一个文件。该文件包含大量constants
,control verifications
,initialization for the DB object
等...
如,
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 ...
由于你有多个“启动”文件并且你需要所有文件来调用inc.php
,从PHP 5.3.0开始,最好的选择似乎是.user.ini
,你可以阅读{{3 }}
关于这个主题的it here!。