1

I have ~100 string variables that need to be available on every webpage of a PHP site. The data will never change at runtime, though in the future I will need multiple sets of the data and to switch between the one in use for a page request. The length of the strings vary from 5 to 600 characters. I'm currently including a file that has the data like this:

$someStuff = "abc";
$otherStuff = "def";
// etc

I am using opcache. How much will this approach benefit from opcache?

I've seen this answer. I could change to using an associative array if the caching benefits were worth doing key lookups. However, it isn't clear to me if using a class with a static array field is better for my situation than declaring variables.

Maybe a function with a static variable is a good idea? Is this the same, better or worse than a static class field?

function getItem ($name) {
    static $items = array("someStuff" => "abc");
    return $items[$name];
}

Maybe a function instead of a variable for each string? Would this be better if not all the strings are used for a given page (which is often the case)?

function someStuff () { return "abc"; }
function otherStuff () { return "def"; }

What is the best solution? The data is needed on every page so I would like to be as efficient as possible, avoid reading from disk/database, etc.

1 Answer 1

2
+100

In practice it makes no difference whether you do something like:

$someStuff = "abc";
$otherStuff = "def";
// ...

or

$constants = array(
    'someStuff' => "abc";
    'otherStuff' = >"def";
    // ...
);

or wrapping this into a static array in a class as per my other answer. Using OPcache will remove the compile overheads and disk I/O overheads. It will intern the string constants so these are effectively statically available to the Zend engine. The class version does a single shallow copy of the array structure, the two other versions will do ~200 opcode execs to initialise ~100 variables, and again the actual interned strings are effectively copied by reference. The engine typically interprets 20-40M opcode execs per second, so do the math: it doesn't matter.

My recommendation is: don't worry about the runtime issues here, just pick the approach which you feel is clearest and most maintainable. Personally, I would use a class autoloaded into from its own config class file, but this is your app and do what is the clearest for you.

BTW, using functions is messy and they do have a runtime cost, and function calls are one of the most expensive PHP operations. References to class constants are quite a lot cheaper, but try benchmarking these yourself. However, again, unless you are referring to these 10k + times per request you aren't going to notice a material difference. Being too clever will only end up with you "shooting yourself in the foot." :-)

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

4 Comments

Thanks for the explanation. I had a feeling it was premature to worry, but it was hard to stop myself since it is on each page. I've gone with the class and static field. Cheers!
BTW, if they are truly constants then read up on PHP Class Constants.
They are indeed. With a couple regex replaces on my source, I switched to class constants. What overhead is there when using opcache and class constants? I'm afraid a microbenchmark could measure the wrong thing or would be difficult to measure the overhead per page load. Besides, it's mostly just to satisfy my curiosity. :)
Two shades of bugger all: a reference to SomeClass::B will generate a single opcode: FETCH_CONSTANT ~0 'SomeClass', 'B' and this is a relatively lightweight opcode.

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.