1

Let me put the requirement to satisfy first

A PHP gateway, and a set of request handlers which uses many constants which currently i am defining in a constants.php with define('conts','value');

I can define this constants in a property file like

const1 =val1
const2 = val2
const3 = val3

in some external file say gateway.properties and load it to define() at runtime. Can this be a one-time action, so that as many threads created by the php can access this constant further, with out reloading it again?

I dont know if this is really possible, i want an expert advice.

Thanks

5 Answers 5

1

I would handle this by stuffing the resulting object in memcached.

There is obviously some overhead with this. You will need to weigh whether or not it makes sense for your situation. For 3 variables, it won't make sense at all. For 300,000, maybe it will. Test it and see.

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

1 Comment

i'm already using memcache for other purposes. this way is good, just wanted to confirm whether i can use it as a constant like CONST_1 instead of $memcache->get(CONST_1)
1

In PHP it is preferred to use .ini files and use

http://php.net/manual/en/function.parse-ini-file.php or php.net/manual/en/function.parse-ini-string.php if you have read the file to a string already.

You can use apc to cache it in memory with a fallback to reading a file if not already cached and then cache it.

<?php
$ini = apc_fetch('configuration');
if (!$ini) {
    $ini = file_get_contents('path/to/ini.ini');
    if ($ini) {
        apc_store('configuration',$ini);
    }
}
$config = parse_ini_string($ini);

Comments

0

You could read each line, parse out the = and define in a for loop.

1 Comment

I think he is trying to save the overhead of having to reload this file every time his application starts up.
0

The easiest way to do this is to store your values in a .ini file, and then read the file in with parse_ini_file(). The ini file would look like:

var1 = 'blah blah blah'
var2 = 'more blah'

PHP reads these files pretty quickly. I suggest that, rather than turn all the values into constants, you store the associative array you get from the .ini file in a single global variable. Let work, same visibility.

If you're really set on caching, you could use the APC cache. It'll save a few miliseconds, but it won't make a difference unless you're talking about a pretty large set of values. And you'll still have to call define() for each value, if you're going to insist on turning them all into constants. Still much faster to save a single global associative array.

Comments

0

Consider using a static class which is globally easy to access.
You can create a class with only public static members and CLASS::init() calls an optional configuration file to replace the variables.
If the value is missing it will stay at it's default.
So you can access the configuration from everywhere using CLASS:$STATIC_VAR

The init function:

            $vars = parse_ini_file(dirname(__FILE__) .'/'. $filename,true,INI_SCANNER_TYPED);
            if ($vars)
            {
                foreach ($vars as $key => $val) 
                {
                        if (property_exists(get_called_class(),$key)) self::$$key=$val;
                }
            }

Comments

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.