mega6382
mega6382

Reputation: 9396

Defining your own magic constants in php?

Is there anyway in php, by which we can define our own magic constants, which value could vary throughout the program and how to define variables with the SUPER GLOBAL SCOPE.

Upvotes: 2

Views: 531

Answers (3)

dotancohen
dotancohen

Reputation: 31471

Just add the variable as an Apache environment variable:

SetEnv foo bar

You could set that in httpd.conf, apache2.conf, or .htaccess.PHP should then be able to access it via one or more of the following methods:

$_SERVER['foo']
$_ENV['foo']
getenv('foo')

Upvotes: 2

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

You cannot define magic constant w/o building own flavour of PHP. But you can mimic this by putting all your "magic" define()s in separate file and including it in each of your script you can use auto_prepend_file config directive. Still, if you think you need something like this, the I'd try to rethink that approach. Whenever "magic" or "global" things comes to play, it's rather indication of need of refactoring.

Upvotes: 0

Zsolt Szilagyi
Zsolt Szilagyi

Reputation: 5006

While you can declare global variables, superglobals are limited to those found in PHP. You might want to keep your data as $_SESSION['mysuperglobal'], althou I suggest more elaborated patterns like a config-singleton, a registry or dependency injection.

Upvotes: 0

Related Questions