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

Asked

Viewed 301 times

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 short snippet of code demonstrates that the intention is to have the settings stored in a PHP file and extract them through include. Another more 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 - There is some performance improvement of one method under another?

3 answers

5


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

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

  • INI
  • Array
  • JSON
  • XML
  • YAML

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

The Symfony2 framework for example allows to make settings by YAML, but even so it slows down the application, since it creates a cache file in Array format with this configuration. (http://symfony.com/doc/master/components/config/introduction.html)

You can see a benchmark here: http://konrness.com/php5/zend_config-benchmark-json-array-ini-xml-yaml/

--

Update:

I’ve also had doubts about what kind of configuration format to use and found the reference I took as a basis at the time (https://web.archive.org/web/20120826210434/http://www.zyxist.com/pokaz.php/formaty_danych_benchmark)

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.

  • Very good explanation of Symphony :). This is to totally take away the prejudice of using yaml

4

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

Note that the way the file is organized, both in one way and the other, and the way that will recover that data can make more difference. A comparison to understand: Which is faster, access the disk or access the memory? It depends on how each access is done. It is possible to make an algorithm so bad in memory 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 by a data structure in the code itself could be better by not having the dependency on an external file that can be modified by someone, even disappearing. And it would have the advantage of allowing someone to change settings by external file. But in PHP the settings are already in external file that can be modified.

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

Usually I’d keep a central file, but it depends on the case. Having a central file loaded may slow down slightly because it will load settings that it does not need at that time. But this is minimal and won’t do any different. It’s purely for organizational reasons. Imagine having to look for settings throughout the application. But I’m not saying that there can’t be cases where decentralizing is positive, I just can’t see one easily.

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

But note that the first case can not be considered exactly setting. I have not seen a concrete case but may be just information needed for something punctual. Though the estate calls config does not mean that it is effectively a configuration like other existing ones. There may be a semantic reason for doing it differently. There may be a need, in the specific case, to make it clear that there needs to be done separately from other configurations.

3

1 - What is the advantage and advantage 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 your content.

On the other hand, use files .ini it becomes clearer to a system administrator for example that that specific file refers to a configuration type.

2 - There is some performance improvement of one method under another?

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

If you use a fopen simple, each request will have to access the disk, may cause bottlenecks.

  • I liked your answer, so I’m giving +1 for collaborating with the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.