0

I was wondering if it possible to add constants to php before any scripts are ran, thus on startup. If this is possible, could it be done with classes etc aswell?

I was thinking in the direction of creating a plugin for php but maybe there is a way simpler way.

I don't mean including a file in every script.

thanks in advance

2
  • Couldn't you use define php.net/manual/en/function.define.php at the beginning of the script? Commented May 22, 2012 at 23:31
  • Could you try explaining your problem in more detail, instead of asking about the implementation details of a potential solution? Commented May 22, 2012 at 23:40

3 Answers 3

3

Not constants as far as I'm aware, but this is ok:

.htaccess

SetEnv MYVAR "hello"

somefile.php

echo $_SERVER['MYVAR']; 

See the Apache docs on SetEnv for more.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

lol Mathew, saw 14.4k under your name and for a second I thought that was what you were dialing in to the internets with .. flash back to '96
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.

2 Comments

This solution fails as soon as there are separate files which need the inc.php to include the inc.php therefore the info has to be know beforehand
@John, added an edit to the answer with a solution for that issue! Also a link to an article on the subject.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.