PHP Classes

Config include

Recommend this page to a friend!

      Log watcher  >  Log watcher package blog  >  10 Steps to properly ...  >  All threads  >  Config include  >  (Un) Subscribe thread alerts  
Subject:Config include
Summary:Config include
Messages:8
Author:José Filipe Lopes Santos
Date:2013-05-29 15:40:54
Update:2013-06-06 05:39:01
 

  1. Config include   Reply   Report abuse  
Picture of José Filipe Lopes Santos José Filipe Lopes Santos - 2013-05-29 15:49:59
Hi !

Usualy in my configuration php file to include (i usualy named it core.inc), i prefer use constants unlesse variables ! :-)

Example
define("NUM_ROWS",15);

  2. Re: Config include   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-05-29 15:54:55 - In reply to message 1 from José Filipe Lopes Santos
Right but you cannot change constants once defined.

The point of using configuration variables as a class is that you just pass the configuration options object to other classes that you need to check the application options.

Since the idea is to change some options for each of the types of environment, those values are better defined as class variables.

  3. Re: Config include   Reply   Report abuse  
Picture of Kai Großjohann Kai Großjohann - 2013-06-03 02:22:30 - In reply to message 2 from Manuel Lemos
I think using constants is safe:

While the script is running, the values of the config settings won't change. Yes, they differ between development and production, but these are two different servers, and two different invocations of the PHP script.

  4. Re: Config include   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-06-03 13:26:07 - In reply to message 3 from Kai Großjohann
That is not the point I am making.

If you followed the point of the article, you can see there is a configuration class that defines default option values for use in the production environment.

Any of the environments that require different values than the default, need to redefine the default values, so constants won't do.

Another problem of global constants (or even global variables) is name collision. If you have different parts of your application using constants with the same names but different meanings, you may run into inconsistency bugs.

That is why configuration options are better encapsulated in a class precisely to avoid name collision.

  5. Re: Config include   Reply   Report abuse  
Picture of Kai Großjohann Kai Großjohann - 2013-06-03 23:35:26 - In reply to message 4 from Manuel Lemos
Sorry, Manuel. Yes, you're right. Not sure how I could have missed that.

  6. Re: Config include   Reply   Report abuse  
Picture of José Filipe Lopes Santos José Filipe Lopes Santos - 2013-06-04 20:09:51 - In reply to message 4 from Manuel Lemos
Ok,

But you as programmer do not define two constants with somme name, right ?

I define these constants to be the most understand easly, like
define("MAX_ROWS",15);

If i want to pass multiples values, i use arrays !
$a = ("car" => "blue", "apple" => "red")
I call
$a["car"]
...

Constancts in most cases is most useful, when you dont want to modify these value ;-)

  7. Re: Config include   Reply   Report abuse  
Picture of Kai Großjohann Kai Großjohann - 2013-06-04 23:13:05 - In reply to message 6 from José Filipe Lopes Santos
José,

here is the problem Manuel wants to solve:

Suppose you have an app with 10 config variables. Suppose that you expect 3 of them to be different in the test environment than in the production environment.

Manuel allows you to have one config file with the 10 production values, and then add another one for the test environment that just overrides the 3 values that are different from production.

Now suppose you use constants and put the 10 production values into a config file. Then you need to copy that file and edit it to create the testing config file. So now you have duplicated 7 values.

Another approach is to split the 7 common values into a common config file, then create one production config with 3 values and a testing config with 3 values. But what if you discover that an additional value becomes environment-specific? Then you have to move it out of the common file into all the environment specific files.

With Manuel's approach you're more flexible: You can put all production values into one file, then override some of them for a specific environment as needed.

That said, if the config files are small, the duplication might not be much of an issue.

Kai

  8. Re: Config include   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-06-06 05:39:01 - In reply to message 7 from Kai Großjohann
Exactly.