elxis sites
we speak your language
Elxis is translated into many languages and we keep local support forums and community sites.
download elxis
elxis 2009.1 Hecate - 10.31mb
Home arrow Guides arrow Developers guides arrow PHP defined constants vs class defined variables
english ελληνικά

PHP defined constants vs class defined variables

Sunday, 15 February 2009

I always had this question, which is better to use for defining language strings in a multi-lingual enviroment such as Elxis CMS, PHP constants or class defined variables? I needed to benchmark PHP to decide which method is better (faster and less memory consuming).

I started by creating 2 identical files. In the first one (test1.php) I defined 5000 constants for my language strings and in the second one (test2.php) I created a class having 5000 defined variables and a magic constructor.

Here is how my test files looks like.

//test1.php
define('var1', 'value1');
define('var2', 'value2');
....
define('var5000', 'value5000');
//test2.php
class language {     public $var1 = 'value1';
    public $var2 = 'value2';
    ....
    public $var5000 = 'value5000';

    public function __construct() {
    }
}

I also added 2 functions to measure the script's speed execution time and the memory usage on both of the files. The measuring points and the help functions were identical on the 2 files.

Benchmark results - Speed

The pages execution time is almost the same for the 2 methods. The class variables page (test2.php) is slightly faster than the constants page (test2.php). On my computer I measured 222-249 ms for the constants page and 220-238 ms for the class variables page. Notice that the speed execution of the test2.php page is almost the same regardless if an object of the class has been initialized or not.

Benchmark results - Memory usage

This is the interesting part. The memory consumption for each script was metered using the memory_get_usage function. I got it's value on the top and the bottom of each script and calculate their difference in order to find the memory that each script consumes. I also used an equivalent of the memory_get_usage function for Windows I found at php.net which show me lower values but the comparison results was the same, so I don't mention these results here in order not to confuse you.

My script occupied 162.24 KB of the server's memory for the constants page. This value was standard in all cases. For the class variables page the memory usage changes dramatically depending on if we have initialize an object for our class or not. With an object initiated the script consumes 579.69 KB of memory. With no object initiated for our class the memory consumption was 2.17 KB. Using the unset function to destroy our object exactly after we created it and free up memory, gives us memory consumption of 128.34 KB. Interesting results, don't you think?

Common logic says that a class has no meaning of existence if it is never get initialized. So for common login, php constants are, by far, a much better solution as they consume 72% less memory.

But what if we initialize our class only when we need it and destroy the object afterwards? What if we split our main class in sub-classes that extends the main class? Such methods can lead to a decrease of the used memory in comparison to the constants method. If the class won't needed to be initialized at all during our script execution the decrease in memory consumption is huge (we spent almost zero memory).

Conclusion

So the answer to the question " which method is better, php constants or class variables", is, in general, PHP constants but can be class variables if we use classes wisely.

Next >