Hello.

I need my class to know my config settings. Basically I insert EVERY singele configuration variable somehow in EVERY class to make it work. Is their a OOP approach that is better? I heard the GLOBAL array is bad in OOP.

So far i have (very clumsy) done two things to pass configuration variables to my class:

Example 1

config.inc.php

$country_id=1;

country.php

<?php
class Country {
	public function show_country($country_id) {
		if($country_id==1) {
			echo 'The country is USA';
		}
	}
}
?>

index.php

<?php
$output = new Country();
include('config.inc.php');
$output->show_country($country_id);
?>

Example 2

config.inc.php

define(COUNTRY_ID,'1');

country.php

<?php
class Country {
	public function show_country($country_id) {
		$country_id = COUNTRY_ID;
		if($country_id==1) {
			echo 'The country is USA';
		}
	}
}
?>

index.php

<?php
$output = new Country();
include('config.inc.php');
$output->show_country();
?>

    That is a good question and I'd love to see other developers comments. I feel at some point you need a "header" file to declare some constants and I'm not above using the constant values within my classes.

    For example, I'm currently working on a larger project with multiple classes working together (databases, security, forms, business logic, etc.) I like having one configuration file where I can easily change system-wide values. the kind of stuff in there is database connection details, notification emails, paths, etc. Since a constant in PHP is globally accessible everywhere, I simply include my constants page on every page in my web application.

    Anybody have a GOOD example on how to do this differently? For the time being, I have no qualms doing it the way I described.

      For constant values, you can put them into an interface, then each class that needs them need only implement that interface (and a class can implement as many interfaces as it needs):

      <?php
      
      interface Constants 
      {
         const COUNTRY = 'USA';
      }
      
      class Test implements Constants
      {
         public function getCountry()
         {
            return self::COUNTRY;
         }
      }
      
      $test = new Test();
      echo $test->getCountry();  // outputs "USA"
      
      ?>
      

        Hey, I like that! I did think for a moment of deriving from a base class with constants. An interface is better because:

        1. You can not instantiate it (only derive from it)

        2. When used in this manner, it ensures that the class that implements it, has the variables it needs!

        3. Enforces public visibility.

        Albeit, not your typical use for interfaces. 😃

          I never thought of it myself until I read this thread and started searching the web, saw that, tried it, and thought, "Cool!". 🙂

          Another approach is to have some sort of configuration class which you instantiate and then pass to each object that needs it. You could even have its constructor populate the data from an XML file, the database, an ini file, etc., allowing the end user to set up configuration parameters in a non-PHP context.

          You could also look into something like the "Registry" pattern, though that may be overkill, depending on what you need to do.

            Thanks, that is great stuff!

            I will try mess around with it some more. Can you tell me a little more about including INI files?

            Some times it is easier to have one file with all the settings so I never have to touch any of my classes.

              PHP comes with [man]ini_parse[/man] for turning an INI file into an array of names/values.

                Write a Reply...