What is the advantage of using array include vs configuration file?

6

I have observed some codes where the programmer does the following:

/** Arquivo A.php */
return array('databaseName' => 'bancoTeste', 'port' => 5208);
/** Arquivo B.php */
$configs = include("A.php");
// Acesso as $configs
echo $configs['databaseName'];

This small snippet of code demonstrates that the intent is to have the settings stored in a PHP file and extract them through include . Another common method is to use a flat configuration file ( .ini for example) and load it via fopen, etc.

1 - What is the advantage and disadvantage of each method?

2 - Is there any performance improvement from one method to another?

    
asked by anonymous 11.02.2015 / 12:15

3 answers

5

In response to your question we can cite several advantages and disadvantages in each type of configuration file in array for example you can create more complex data structures than using INI, but in contrast INI is more readable than an array .

But basically the use goes of the taste / need of the solution you are implementing, some of the configuration formats that I can quote is:

  • INI
  • Array
  • JSON
  • XML
  • YAML

In terms of performance Array has the advantage of being a native type composed of PHP. But as I said nothing prevents you from using another method / format to configure your application.

The Symfony2 framework, for example, allows you to make configurations for YAML, but it does not slow the application as it creates an Array format cache file with this configuration. ( link )

You can see a benchmark here: link

-

Update:

I already had doubts as to what type of configuration format to use and I found the reference I took as a base at the time ( link )

Since the order from the fastest to the slowest is:

  • Serialized Array
  • PHP script
  • INI
  • XML
  • YAML

But nothing prevents you from using a more readable format for your configuration (YAML) and using the strategy of transforming into an array.

    
11.02.2015 / 12:47
4

There is no performance improvement that really makes a difference. If measuring is likely, but it is not certain, it has some advantage to one side or the other. If you really want to know, take a benchmark test. I will not do it because it is not worth even being simple to do. It does not matter, it does not change anything in any program.

Note that the way the file is organized, both in one form and the other, and the way it will recover this data can make more difference. A comparison to understand: What is faster, access the disk or access memory? It depends on how each access is made. It is possible to make such a poor memory algorithm that it is slower than accessing the same data on disk. It is an extreme example but it is possible. The moral of the story is that algorithms are more important.

In general in other languages I would say that setting up a data structure in the code itself might be better because it does not depend on an external file that can be modified by someone, even disappear. And it would have the advantage of allowing someone to change the settings through the external file. But in PHP the settings are already in an external file that can be modified.

What I can see differently and this is important is that if you just return a setting and have all of them in a central file (no matter what format and how it is loaded) it changes the organization.

In general I would have a central file but it depends on the case. Having a loaded central file may be slightly slower because it will load settings that it does not need at that time. But this is minimal and will not make any difference. It's purely for organizational reasons. Imagine having to search for settings throughout the application. But I do not say that there can be no cases that decentralize is positive, I just can not see one easily.

Other than that, unless I see an explicit justification, I would say it's a matter of style.

But note that the first case may not be considered exactly configuration. I have not seen a concrete case but it may just be a necessary information for something punctual. Although the property calls config does not mean that it is actually a setting like other existing ones. There may be a semantic motif to do differently. There may be a need, in this case, to make it clear that there needs to be done in isolation from other settings.

    
11.02.2015 / 12:43
3

1 - What is the advantage and disadvantage of each method?

The advantage of the first approach is simplicity: you do not need to open the file resource, read the data, convert to array, close to use of your content.

On the other hand, using .ini files is clearer for a system administrator, for example, that particular file refers to a type of configuration.

2 - Is there any performance improvement from one method to another?

Nothing significant, but with the first approach PHP can store the file in memory through the OPcache , making loading on subsequent requests faster.

In case of using a simple fopen , each request will have to access the disk, which may cause bottlenecks.

    
11.02.2015 / 12:47