Note that there are some explanatory texts on larger screens.

plurals
  1. POBest performance for loading settings in PHP?
    text
    copied!<p>My latest idea for do settings across my php project I am building was to store all my settings in a config PHP file, the php file will just return an array like this...</p> <pre><code>&lt;?php /** * @Filename app-config.php * @description Array to return to our config class */ return array( 'db_host' =&gt; 'localhost', 'db_name' =&gt; 'socialnetwork', 'db_user' =&gt; 'root', 'db_password' =&gt; '', 'site_path' =&gt; 'C:/webserver/htdocs/project/', 'site_url' =&gt; 'http://localhost/project/', 'image_path' =&gt; 'C:/webserver/htdocs/fproject/images/', 'image_url' =&gt; 'http://localhost/project/images/', 'site_name' =&gt; 'test site', 'admin_id' =&gt; 1, 'UTC_TIME' =&gt; gmdate('U', time()), 'ip' =&gt; $_SERVER['REMOTE_ADDR'], 'testtttt' =&gt; array( 'testtttt' =&gt; false ) ); ?&gt; </code></pre> <p>Please note the actual config array is MUCH MUCH larger, many more items in it...<br> Then I would have a Config.class.php file that would load my array file and use the magic method __get($key). I can then autoload my config class file and access any site settings like this... </p> <pre><code>$config-&gt;ip; $config-&gt;db_host; $config-&gt;db_name; $config-&gt;db_user; </code></pre> <p>So I realize this works great and is very flexible, in my class I can have it read in a PHP file with array like I am doing now, read INI file into array, read XML file into array, read JSON file into array. So it is very flexible for future projects but I am more concerned about performance for this particular project that I am working on now, it will be a social network site like facebook/myspace and I have had one before prior to this project and once I got around 100,000 user's performance became very important. So I am not "micro-optimizing" or "premature optimizing" I am stricly looking to do this the BEST way with performance in mind, it does not need to be flexible as I will only need it on this project. </p> <p>So with that information, I always read about people trying to eliminate function calls as much as possible saying <strong>function calls cause more overhead</strong>. SO I am wanting to know from more experienced people what you think about this? I am new to using classes and objects in PHP, so is calling $config->db_user; as costly as calling a function in procedural like this getOption('db_user'); ? I am guessing it is the same as every time I would call a setting it is using the __get() method. </p> <p>So for best performance should I go about this a different way? Like just loading my config array into a bootstrap file and accessing items when I need them like this...</p> <pre><code>$config['db_host']; $config['db_username']; $config['db_password']; $config['ip']; </code></pre> <p>Please give me your thoughts on this without me having to do a bunch of benchmark test</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload